''' 20210923 R. Dawes ''' import tkinter as tk window = tk.Tk() # create an object that can be displayed on the screen window.title("My First GUI") # give it a title window.geometry("1200x800") # width x height in pixels #add a label - a box with text on it label_1 = tk.Label(window,text="This is a label") # the "window" in this attaches the new label to the displayable object created above #add an entry box to accept input entry_1 = tk.Entry(window, width = 10) # width in characters, not pixels #add a text box to show output text_1 = tk.Text(window,height = 5, width = 30) # add a button - a clickable object # add a function to react to button_1 # This function must be defined BEFORE the button is defined def button_1_clicked(): x = entry_1.get() text_1.insert(tk.END,"You entered\n\n"+x) # the first parameter of the insert function indicates where in the Text box # the new string is to be placed. "tk.END" is a special indicator that means # "after all the current content of the Text box". button_1 = tk.Button(window, text = "Click Me!", command = button_1_clicked) # note that when we specify the function to be run when the button is clicked, we # do NOT include parentheses () after the function name # The objects we have just created will not be displayed unless we tell tkinter where # to place them in the displayable object called "window" #~ # place widgets using pack - quick and simple, basically useless #~ label_1.pack() #~ entry_1.pack() #~ text_1.pack() #~ button_1.pack() # place widgets using pack - semi-useful #~ label_1.pack(side = tk.TOP) #~ entry_1.pack(side = tk.LEFT) #~ text_1.pack(side = tk.LEFT) #~ button_1.pack(side = tk.BOTTOM) # place widgets in a flexible grid label_1.grid(row = 0, column = 0) entry_1.grid(row = 1, column = 0) text_1.grid(row = 1, column = 1) button_1.grid(row = 2, column = 2) window.mainloop() # this command displays the object and all the widgets we have # positioned on it. It will now loop forever, waiting patiently for # our actions.