# Encoding = utf-8import tkinter as TK # define window = TK. TK () # define the window title window. title ("My window") # define the window size window. geometry ("400x200") # define the label text variable, label_var = TK. stringvar () # define a labalelabel = TK. label (window, BG = "yellow", width = 4, Height = 2, textvar = label_var) # Place the label on the window. pack () def print_selection (): "implementation: When you select the option of the list box and click the button, explicitly select the content to the tag: return: "# Get the selected content value of the List = list_box.get (list_box.curselection () # Set the content to label_var.set (value) in the label text box # define a button, click the button to execute the print_selection function. Button = TK. button (window, text = "", width = 10, Height = 2, command = print_selection) button. pack () # define a text variable list_box_var = TK. stringvar () # set the value of the text variable list_box_var.set (1, 2, 4) # define a list_box = TK. listBox (window, listvar = list_box_var) # a certain list, and insert the list content to the list_items = ["A", "B", "C ", "D"] For item in list_items: list_box.insert ("end", item) # Insert at the end # insert list_box.insert (1, "first") list_box.insert (2, "second") # Delete the list_box.delete (2) list_box.pack () # loop window by index. mainloop ()
Tkinter learning _ ListBox