Python GUI instance learning and pythongui instance
Before learning this article, if you still do not understand the basic content of GUI programming in Python, we recommend a related article: A simple tutorial on GUI programming using TK in Python
It is easy to write a simple interface. Even if you do not know anything, this text is reproduced in the simplest interface writing. The next text introduces the simple but specific application of TK.
Create a window in python and display it.
from Tkinter import * root = Tk() root.mainloop()
The main window is displayed in three rows. Root is a variable name, which represents the main window. In the future, when creating a control, specify the window in which the control is created, which is represented by the root. Tk () is a function in the Tkinter Library (in fact, it is a class constructor that constructs an object ). Mainloop is a member function of the main window, that is, to let the root work and start receiving mouse and keyboard operations. Now you can zoom in and close the window with the mouse. Note that the title of the window is tk. We can make some modifications.
root= Tk(className='bitunion')
Then the title of the window becomes bitunion. What we need to do below is to fill in the content of this window and let it have something. Add a label first. The label is a line of words.
from Tkinter import * root = Tk(className='bitunion') label = Label(root) label['text'] = 'be on your own' label.pack() root.mainloop()
We are surprised to find that the window has become smaller, but there is a line more. It is because something has been put in the window, and python Tkinter is very intelligent. It can automatically scale according to the content, instead of manually specifying absolute coordinates like a traditional windows program. Label is a variable. However, this variable represents a tag, that is, a line of words. The label is created using the Label, and the Label parameter is root, indicating that the control is a member control of the root main window, or a child window. Label ['text'] indicates that the text attribute of this label is 'Be on your own ', that is, the text content. Label. pack is as confusing as root. mainloop, but profound as it is. Now, you can simply understand the label display function. Because you remove the pack, you will not be able to see anything. In fact, pack is related to the layout and layout of controls.
You can add a button to add more content. The method is similar. Look at it:
from Tkinter import * root = Tk(className='bitunion') label = Label(root) label['text'] = 'be on your own' label.pack() button = Button(root) button['text'] = 'change it' button.pack() root.mainloop()
But the button replaces the label and the Button replaces the Label. Note that the buttons and labels are provided by libraries such as Tkinter. The difference between the case sensitivity of buttons and buttons is just a coincidence. You can give the variable a name at will, however, buttons and labels are things to remember. They are often used when writing code. However, you will be disappointed when you click the button, because there is no response. However, you did not tell the computer what kind of reaction is needed to click such a button to give feedback to the user. However, this task only requires one row to specify the response, but a new function is required to describe the specific response.
from Tkinter import * def on_click(): label['text'] = 'no way out' root = Tk(className='bitunion') label = Label(root) label['text'] = 'be on your own' label.pack() button = Button(root) button['text'] = 'change it' button['command'] = on_click button.pack() root.mainloop()
Button ['command'] = on_click indicates that the on_click function is used to process the click attribute of the button. The on_click function is also very concise, just to reset the text of the label. This completes the processing of an event message. If you use C for writing, you need to write a code that is longer and harder to understand. In addition, are you surprised to see the label variable in on_click? The label variable is not defined before on_click. If I write a program like this in C, the compiler will tell me what went wrong. But how does python know that the label variable exists and no error is reported? In fact, when you write python, you don't need to know whether it exists. You just need to find the label when running it. The frontend and backend relationships are associated by time, rather than the relationship between the Code going forward and going forward. Here, because label = Label (root) is executed before on_click, when on_click is executed, label is a defined variable. What if there is no definition? Report the error.
Last example:
from Tkinter import * def on_click(): label['text'] = text.get() root = Tk(className='bitunion') label = Label(root) label['text'] = 'be on your own' label.pack() text = StringVar() text.set('change to what?') entry = Entry(root) entry['textvariable'] = text entry.pack() button = Button(root) button['text'] = 'change it' button['command'] = on_click button.pack() root.mainloop()
This is complicated. There is a StringVar in it. This represents a string, but it is different from a general string. Generally, the 'dfsdf 'string is unchangeable. You can only specify variables as different strings, but the content of the string itself cannot be changed. StringVar is a variable string. So set and get are used to set and obtain its content. The entry (single-line input box) requires such an attribute to set and receive the content of its input box.
Use Tkinter to implement a simple GUI program. When you click the click button, the 'hello world' will be printed on the terminal ':
_ Author _ = 'fyby'
From Tkinter import * # introduce the Tkinter Toolkit
Def hello ():
Print ('Hello world! ')
Win = Tk () # define a form
Win. title ('Hello World') # define the form title
Win. geometry ('400x200') # defines the size of the form, which is pixels
Btn = Button (win, text = 'click me', command = hello)
# Do not write it as hello (). If it is hello,
# The hello function will be called in mainloop,
# Instead of starting an event when the button is clicked
Btn. pack (expand = YES, fill = BOTH) # pack the button and fill it with the entire form (only the pack component instance can be displayed)
Mainloop () # enter the main loop and the program runs
When we write a large program, we 'd better divide the code into one or several classes. Let's look at the Hello World example.
#-*-Encoding = UTF-8-*-_ author _ = 'fyby' from Tkinter import * class App: def _ init _ (self, master ): # input a parent component (master) in the constructor, create a Frame component, and display frame = Frame (master) frame. pack () # create two buttons as part of the frame self. button = Button (frame, text = "QUIT", fg = "red", command = frame. quit) self. button. pack (side = LEFT) # Here side is LEFT, which indicates placing it to the leftmost self of the remaining space of the frame. hi_there = Button (frame, text = "Hello", command = self. say_hi) self. hi_th Ere. pack (side = LEFT) def say_hi (self): print "hi there, this is a class example! "Win = Tk () app = App (win) win. mainloop ()
After reading the two boring Hello World examples above, let's look at a little bit of Perfect. Menu component, draw a decent program shell.
#-*-Encoding = UTF-8-*-_ author __= 'fyby' fromTkinterimport * classApp: def _ init _ (self, master ): # input a parent component (master) in the constructor, create a Frame component, and display frame = Frame (master) frame. pack () # create two buttons as part of the frame self. button = Button (frame, text = "QUIT", fg = "red", command = frame. quit) self. button. pack (side = LEFT) # Here side is LEFT, which indicates placing it to the leftmost self of the remaining space of the frame. hi_there = Button (frame, text = "Hello", command = self. say_hi) self. hi_there.pack (side = LEFT) defsay _ Hi (self): print "hithere, thisaclassexample! "Win = Tk () app = App (win) win. mainloop ()
This program is still a bit boring, because we just created a top-level navigation menu and clicked it to output hello in the terminal. Next we will create a drop-down menu, in this example, three top-level menus are created, each of which has a drop-down menu (created using add_command, add the add_cascade Method to the upper menu), bind a hello function to each drop-down option, and print hello in the terminal.
Root. quit is the instance that exits this Tk.
#-*-Encoding = UTF-8-*-_ author _ = 'fyby' from Tkinter import * root = Tk () def hello (): print ('hello ') def about (): print ('I am a developer') menubar = Menu (root) # create a drop-down Menu File and add it to the top Menu bar filemenu = Menu (menubar, tearoff = 0) filemenu. add_command (label = "Open", command = hello) filemenu. add_command (label = "Save", command = hello) filemenu. add_separator () filemenu. add_command (label = "Exit", command = root. quit) menubar. add_cascade (label = "File", menu = filemenu) # create another drop-down Menu Editeditmenu = menu (menubar, tearoff = 0) editmenu. add_command (label = "Cut", command = hello) editmenu. add_command (label = "Copy", command = hello) editmenu. add_command (label = "Paste", command = hello) menubar. add_cascade (label = "Edit", menu = editmenu) # create a drop-down Menu Helphelpmenu = menu (menubar, tearoff = 0) helpmenu. add_command (label = "About", command = about) menubar. add_cascade (label = "Help", menu = helpmenu) # display menu root. config (menu = menubar) mainloop ()
After writing this, I almost had a general impression on Tkinter. It is quite simple to use Tkinter to draw a GUI in Python. Expand the example above and combine it with the Label. When you click "about", print the content of "About" on the form instead of output on the terminal. Modify the about function slightly. After you click about, a label is drawn and its content is displayed in the rendering frame by calling the about function.
Def about (): w = Label (root, text = "\ nfuyunbiyi \ nfyby not available girlfriend \ nhttp: // www.programup.com") w. pack (side = TOP)
Kinter provides various controls, such as buttons, labels, and text boxes, used in a GUI application. These controls are usually called controls or components.
Currently, there are 15 Tkinter components. We propose these components and a brief introduction in the following table:
Standard attributes:
Standard attributes are the common attributes of all controls, such as the size, Font, and color.
Geometric management:
The Tkinter control has a specific geometric state management method to manage the organization of the entire control area. Here is the geometric management class exposed by Tkinter: Package, grid, and location.