This post was last edited by Changjianze1 on 2019-2-20 13:23This content was originally created by EEWORLD forum netizen Changjianze1. If you need to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source Let's review first. This is the most basic. Every program needs Tkinter. The program only needs to do three things: The smallest [Tkinter] program (based on Python3, Python2 is slightly different) from tkinter import * # import the Tkinter module root = Tk() # create a root window root.mainloop() # create an event loop Four input boxesEntry 0000] English: The input box Entry is one of the most commonly seen interface controls. For example, it is needed to enter the login account password, such as the serial port assistant to enter the data to be sent, etc., so let's take a look at the syntax of this control. The syntax format is as follows: w = Entry( master, option, ... ) option is generally used: bg, font, show, etc. Example:
5. Text box Text The text box Text is also a very commonly used control. For example, in the serial port assistant that everyone often uses, Text is often used to display the data received by the serial port. When the serial port receives data, text displays the received data on the interface, so let's learn it! ! The Text control can display web links, pictures, HTML pages, and even CSS style sheets. Application method: 1. Method to set the text of the Python Tkinter Text control text.insert(index,string) index = xy format, x represents the row, y represents the column Insert data into the first row, text.insert(1.0,'hello world') 2. Method to clear the text of the Python Tkinter Text control # Idea: clear from the first row to the last row text.delete(1.0,Tkinter.END) Example:
from tkinter import * # import the Tkinter module root = Tk() # create a root window text = Text(root, width=50, height=10) #30 means the width of 30 average characters, and the height is set to two lines text.pack() text.insert(INSERT, '0x55 ') #INSERT indicates the position of the input cursor. The input cursor after initialization is in the upper left corner by default text.insert(INSERT, '0x66 ') text.insert(INSERT, '0x77 ') text.insert(INSERT, '0x88 ') text.insert(INSERT, '0x99 ') text.insert(INSERT, '0xAA ') text.insert(INSERT, '0xBB ') text.insert(INSERT, '0xCC ') text.insert(INSERT, '0xDD ') text.insert(END, 'show is over') root.mainloop()
6. Layout control Several of the most important controls have been basically learned. I mainly want to imitate and make a simple interface. Now let's start looking at some page control layouts. Tkinter has three geometric layout managers, namely: pack layout, grid layout, and place layout. Pack layout has been used in the previous sections, and complex applications are not used for the time being. Mainly look at the grid layout. The grid layout is also called the grid layout, which is the most recommended layout. Most programs have rectangular interfaces. We can easily divide it into a grid of several rows and columns, and then place components in the grid according to the row and column numbers. When using the grid layout, you need to specify two parameters in it, using row to represent the row and column to represent the column. It should be noted that the serial numbers of row and column start from 0. from tkinter import * # import the Tkinter module root = Tk() # create a root window root.title('title') root.geometry('300x200') label1 = Label(root, text = 'i am label1') label1.grid(row=0) label2 = Label(root, text = 'i am label2') label2.grid(row=0, column=1) root.mainloop()