11from icedpygui import IPG , IpgTextParams , IpgRadioDirection , IpgRadioParams
22from icedpygui import IpgButtonParams , IpgProgressBarParams
33from icedpygui import IpgColumnAlignment
4+ from icedpygui import TableWidget , TableRowHighLight
45import random
56
67"""
@@ -375,14 +376,13 @@ def text_on_input(self, _input_id, data):
375376
376377 def construct_window_2 (self ):
377378 self .ipg .add_window (self .wnd_2 , "Demo Window 2 - Iced Wrapped in Python" ,
378- width = 500 , height = 500 ,
379+ width = 600 , height = 500 ,
379380 pos_x = 650 , pos_y = 25 )
380381
381382 self .ipg .add_column (window_id = self .wnd_2 , container_id = self .l_col_2 ,
382383 width_fill = True , align_items = IpgColumnAlignment .Center )
383384
384- # A date picker is defined and the results are put in a text widget.
385-
385+ # A date picker is defined and the results are put in a text widget.
386386 def construct_date_picker (self ):
387387 self .ipg .add_date_picker (self .l_col_2 , on_submit = self .date_selected )
388388
@@ -392,35 +392,85 @@ def construct_date_picker(self):
392392 def date_selected (self , _date_id , date ):
393393 self .ipg .update_item (self .date_text_id , IpgTextParams .Content , f"You selected: { date } " )
394394
395- # A table is defined with 4 columns of random items.
395+ # A table is defined with 6 columns of widgets and random items.
396396 # Rust does not allow types to be mixed in a list.
397397 # Therefore, if a mixed list is needed, convert it to a list[str].
398398 # The gui converts the list to strings anyway.
399- # Width and height are required for the table.
399+ # Width and height are required for the table.
400400 def construct_table (self ):
401- col1 : list [float ] = []
402- col2 : list [str ] = []
403- col3 : list [int ] = []
404- col4 : list [bool ] = []
405-
406- for i in range (0 , 25 ):
401+ # Initialize the lists.
402+ col0 = []
403+ col1 = []
404+ col2 = []
405+ col3 = []
406+ col4 = []
407+ col5 = []
408+ col6 = []
409+
410+ # Add some random data of different types
411+ for i in range (0 , 20 ):
412+ # labels for the button widget
413+ col0 .append ("Button" )
414+ # labels for the checkboxes
415+ col1 .append ("" )
416+ # make a selectable text
417+ col2 .append ("Select Me" )
407418 # make a float random number
408- col1 .append (random .randrange (10 , 99 ) + random .randrange (10 , 99 ) / 100 )
409- col2 .append (random .choice (["one" , "two" , "3" , "four" , "5" , "six" , "seven" ]))
410- col3 .append (random .randrange (10 , 99 ))
411- col4 .append (random .choice ([True , False ]))
412-
413- # Note this is a list of dictionaries not a single dictionary
414- data = [
415- {"Floats" : col1 },
416- {"Strings" : col2 },
417- {"Integers" : col3 },
418- {"Booleans" : col4 }
419- ]
420-
421- table_id = self .ipg .add_table (parent_id = self .l_col_2 ,
422- title = "My Table" , data = data ,
423- width = 450.0 , height = 300.0 )
419+ col3 .append (random .randrange (10 , 99 ) + random .randrange (10 , 99 ) / 100 )
420+ col4 .append (random .choice (["one" , "two" , "three" , "four" , "five" , "six" , "seven" ]))
421+ col5 .append (random .randrange (10 , 99 ))
422+ col6 .append (random .choice ([True , False ]))
423+
424+ # Create the table, the requirement is a list of dictionaries.
425+ # Rust does not have dictionaries but a similar type is called a HashMap.
426+ # The reason for the list of dictionaries is that you cannot extract a
427+ # mixed dictionary into a Rust HashMap. The HashMap has to have predefined
428+ # types. In this case they are <>String, Vec<Widgets>, <String, Vec<f64>>, <String, Vec<String>>,
429+ # <String, Vec<i64>>, and <String, Vec<bool>>. As one iterates through the list,
430+ # each type is tested to see if it can be extracted in one of the types above. If found,
431+ # the extraction occurs and life is wonderful. If no existing type is found, then an error occurs.
432+ # Currently, not every variation is covered but that can be improved in future versions.
433+ # This probably covers the vast majority of needs. If you need that mixed column, convert
434+ # the list to a string. When the final version is displayed, it's converted to a string anyway.
435+ data = [{"Button" : col0 },
436+ {"ChkBox" : col1 },
437+ {"Selectable" : col2 },
438+ {"Col3" : col3 },
439+ {"Col4" : col4 },
440+ {"Col5" : col5 },
441+ {"Col6" : col6 }]
442+
443+
444+ # The column widgets are prepared
445+ btn_widgets = []
446+ chkbox_widgets = []
447+ selectable = []
448+
449+ for _ in range (0 , len (col0 )):
450+ btn_widgets .append (TableWidget .Button )
451+ chkbox_widgets .append (TableWidget .Checkbox )
452+ selectable .append (TableWidget .Text )
453+
454+ # The table is added.
455+ self .ipg .add_table (self .l_col_2 , "My Table" , data ,
456+ width = 600.0 , height = 300.0 ,
457+ row_highlight = TableRowHighLight .Lighter ,
458+ table_length = len (col1 ),
459+ widgets_using_columns = {0 : btn_widgets , 1 : chkbox_widgets , 2 : selectable },
460+ on_press_button = self .widget_button ,
461+ on_toggle_checkbox = self .widget_checkbox ,
462+ on_enter = self .on_text_enter ,
463+ )
464+
465+ def widget_button (self , tbl_id : int , wid_index : tuple [int , int ]):
466+ print (tbl_id , wid_index )
467+
468+ def widget_checkbox (self , tbl_id : int , wid_index : tuple [int , int ], is_checked : bool ):
469+ print (tbl_id , wid_index , is_checked )
470+
471+ def on_text_enter (self , tbl_id , text_index : tuple [int , int ]):
472+ print (tbl_id , text_index )
473+
424474
425475
426476demo = Demo ()
0 commit comments