--- slideOptions: theme: white --- # Chapter 19 - Basic GUI Programming using `tkinter` (II) ###### tags: `GUI Programming` --- ## Input text for one line To create a text box to input a line of text, `Entry()` can be used. We can use `place()` to place the text box. ```python= import tkinter root = tkinter.Tk() # Create the window root.title("The first textbox") # Name the title root.geometry("400x200") # Configurate the size in pixel entry = tkinter.Entry(width=20) entry.place(x=10, y=10) root.mainloop() # Display the window ``` By running the program, a text box is placed on the window. We can change the parameter `width` in `Entry()` to specify the number of characters. ![](https://hackmd.io/_uploads/BkAav5hvn.png) ### Operation of the string by `Entry` We can use `get()` to get the string from `Entry`. Please input and run the following program. ```python= import tkinter def click_btn(): txt = entry.get() button["text"] = txt root = tkinter.Tk() # Create the window root.title("The first textbox") # Name the title root.geometry("400x200") # Configurate the size in pixel entry = tkinter.Entry(width=20) entry.place(x=10, y=10) button = tkinter.Button(text="Get text", command=click_btn) button.place(x=20, y=100) root.mainloop() # Display the window ``` ![](https://hackmd.io/_uploads/rksgq53v2.png) ## Input text for multiple lines The input multiple lines, we can use `Text()` command. Please input and run the following program. ```python= import tkinter def click_btn(): text.insert(tkinter.END, "Monsters appear!") root = tkinter.Tk() # Create the window root.title("The first textarea")# Name the title root.geometry("400x200") # Configurate the size in pixel button = tkinter.Button(text="Message", command=click_btn) button.pack() text = tkinter.Text() text.pack() root.mainloop() # Display the window ``` ![](https://hackmd.io/_uploads/Hy-asc3Dn.png) The program uses `pack()` to palce the text are created by the `Text()` command. If `place()` is used, we can specify the position and the size. For example, we can specify `width` and `height`. ```python! text = tkinter.Text() text.place(x=20, y=50, width=360, height=120) ``` The text area can add text by `insert()` just like in line 4. This time, the text are added at the end of the text area by using `tkinter.END`. ## Radio Button We can use `Checkbutton()` to create radio button. Please input and run the following program. ```python= import tkinter root = tkinter.Tk() # Create the window root.title("The first Checkbox") # Name the title root.geometry("400x200") # Configurate the size in pixel cbtn = tkinter.Checkbutton(text="Check Button") cbtn.pack() root.mainloop() # Display the window ``` ![](https://hackmd.io/_uploads/rkPGlsnv2.png) ### Get checked status We need to write a more complicated program to get the checked status. We can use `BooleanVar()` command to get the checked status. Please input and run the following program. ```python= import tkinter root = tkinter.Tk() # Create the window root.title("The first Checkbox") # Name the title root.geometry("400x200") # Configurate the size in pixel cval = tkinter.BooleanVar() cval.set(True) cbtn = tkinter.Checkbutton(text="Check Button", variable=cval) cbtn.pack() root.mainloop() # Display the window ``` ![](https://hackmd.io/_uploads/S1ZAGs3v2.png) An object is created by `BooleanVar()` on line 5, and then set the status as `True`. `True` means checked, and `False` means unchecked. After creating check button on line 7, use `variable` in `BooleanVar()` object to create the relationship of the `BooleanVar()` and the check button. To get the check statues, we can use `get()` of the `BooleanVar()` to get the check status. Please input and run the following program. ```python= import tkinter def check(): if cval.get() == True: print("Checked") else: print("Unchecked") root = tkinter.Tk() # Create the window root.title("The first Checkbox") # Name the title root.geometry("400x200") # Configurate the size in pixel cval = tkinter.BooleanVar() cval.set(False) cbtn = tkinter.Checkbutton(text="Check Button", variable=cval, command=check) cbtn.pack() root.mainloop() # Display the window ``` ![](https://hackmd.io/_uploads/HkROSs2wh.png) ![](https://hackmd.io/_uploads/B1L3Sj2Dn.png) ## Message Box We can use the module `tkinter.messagebox` to load the message box. Please input and run the following program. ```python= import tkinter import tkinter.messagebox def click_btn(): tkinter.messagebox.showinfo("Info", "Button Clicked.") root = tkinter.Tk() # Create the window root.title("The first Msgbox") # Name the title root.geometry("400x200") # Configurate the size in pixel btn = tkinter.Button(text="test", command=click_btn) btn.pack() root.mainloop() # Display the window ``` The following box will be shown after clicking the button. ![](https://hackmd.io/_uploads/rkO5jRaw2.png) This program uses `showinfo()` command to show the message. There are many types of messgae, as shown below. |Command|Description| |:------|:----------| |`showinfo()`|Show information message box| |`showwarning()`|Show warning message box| |`showerror()`|Show error message box| |`askyesno()`|Show the message box with "Yes" and "No" button| |`askofcancel()`|Show the message box with "OK" and "Cancel"| ## Using `after()` command We can use `after()` command to perform immediate process. The program can count up automatically. Please input and run the following program. ```python= import tkinter tmr = 0 # Declare tmr as 0 def count_up(): global tmr tmr += 1 label["text"] = tmr root.after(1000, count_up) root = tkinter.Tk() # Create the window label = tkinter.Label(font=("Times New Roman", 80)) label.pack() root.after(1000, count_up) # Call count_up after 1 second root.mainloop() # Display the window ``` After the program is run, the value inside the window will increase once a second (1000 ms). `global` can change the variable outside of the scope of the function within the function. ![](https://hackmd.io/_uploads/S1HrJkAP3.png) For line 4-8, it defines the function `count_up`, and this function perform the action immediately by using `after()` command. The syntax of `after()` is shown below. ```python! after(millisecond, function_name) ``` The `function_name` in the `after()` command do not need to add `()`. ## Reference https://tkdocs.com/pyref/