In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to understand Python GUI programming", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn "how to understand Python GUI programming".
Catalogue
Python GUI programming
Project learning,
1. Roll call machine
a. Navigation bar
b. Main body
c. Logic
Python GUI programming
0. Create window
Import tkinter as tk# create a window root = tk.Tk () root.mainloop () # step 2, name the visualization of the window root.title ('My Window') # set the minimum and maximum root.minsize (300300) root.maxsize (500500)
The use of 1.Label and Button
#! / usr/bin/env python#-*-coding: utf-8-*-# author: Hongwei import tkinter as tk # needs to import # step 1, instantiate object, create window windowwindow = tk.Tk () # step 2, name the window window.title ('My Window') # step 3 Set the size of the window (length * width) window.geometry ('500x300') # the multiplication here is small x # step 4, set the label var = tk.StringVar () # on the graphical interface and set the content of the label tag to the character type Using var to receive the outgoing content of the hit_me function is used to display l = tk.Label (window, textvariable=var, bg='green', fg='white', font= ('Arial', 12), width=30, height=2) # description: bg is the background, fg is the font color, font is the font, width is long, height is high, where the length and height are the length and height of the character For example, height=2, where the tag is as high as 2 characters, l.pack () # defines a function (the content is written freely), which can be called when you click the Button button. Call the command parameter command= function name on_hit = Falsedef hit_me (): global on_hit if on_hit = = False: on_hit = True var.set ('you hit me') else: on_hit = False var.set ('') # step 5 Place the Button button b = tk.Button (window, text='hit me', font= ('Arial', 12), width=10, height=1, command=hit_me) b.pack () # in the window interface, and the main window loops to display window.mainloop ()
The use of 2.Entry and Text
Entry is a single-line text input field provided in the tkinter class that is used to enter and display one line of text and collect keyboard input (similar to text in HTML).
#! / usr/bin/env python#-*-coding: utf-8-*-# author: Hongwei import tkinter as tk # needs to import # step 1, instantiate object, create window windowwindow = tk.Tk () # step 2, name the window window.title ('My Window') # step 3 Set the size of the window (length * width) window.geometry ('500x300') # the multiplication here is small x # step 4 Set the input box control entry on the graphical interface and place the control E1 = tk.Entry (window, show='*', font= ('Arial', 14)) # display in ciphertext form E2 = tk.Entry (window, show=None, font= (' Arial', 14)) # display in clear text form e1.pack () e2.pack () # step 5, the main window loop displays window.mainloop ()
Text is a multiline text area provided in the tkinter class that displays multiline text that can be used to collect (or display) user input text (similar to textarea in HTML), format text display, allow you to display and edit text with different styles and attributes, and support embedded images and windows.
#! / usr/bin/env python#-*-coding: utf-8-*-# author: Hongwei import tkinter as tk # needs to import # step 1, instantiate object, create window windowwindow = tk.Tk () # step 2, name the window window.title ('My Window') # step 3 Set the size of the window (length * width) window.geometry ('500x300') # the product here is small x # step 4, set the input box control entry box on the graphical interface and place e = tk.Entry (window, show=None) # displayed in clear text e.pack () # step 5, define the two functions insert_point and insert_end when triggering the event (note: because the order of Python execution is from top to bottom So the function must be placed on top of the button) def insert_point (): # insert input var = e.get () t.insert ('insert', var) def insert_end (): # insert input var = e.get () t.insert (' end', var) # step 6 at the end of the text box Create and place two buttons to trigger b1 = tk.Button (window, text='insert point', width=10, height=2, command=insert_point) b1.pack () b2 = tk.Button (window, text='insert end', width=10, height=2, command=insert_end) b2.pack () # step 7, create and place a multiline text box text to display Specify height=3 as the text box is a three-character height t = tk.Text (window, height=3) t.pack () # step 8, the main window loops window.mainloop ()
Entry.get () gets the text content of the input.
Text.insert ('insert',content) insert content at mouse focus
Insert content at the end of Text.insert ('end',content).
3.Grid grid layout
#! / usr/bin/env python#-*-coding: utf-8-*-# author: Hongwei import tkinter as tk # needs to import # step 1, instantiate object, create window windowwindow = tk.Tk () # step 2, name the window window.title ('My Window') # step 3 Set the size of the window (length * width) window.geometry ('500x300') # the multiplication here is small x # step 4, grid placement method for i in range (3): for j in range (3): tk.Label (window, text=' (' + str (I) +','+ str (j) +') .grid (row=i, column=j, padx=10, pady=10, ipadx=10, ipady=10) # step 5, the main window loop shows window.mainloop ()
4.Frame framework
Frame: the framework, which is used to hold and place other GUI elements, is a container that separates small areas on the Windows. It divides the Windows into different regions and then stores different parts. At the same time, a Frame can also be divided into two Frame, Frame can be thought of as a container.
#! / usr/bin/env python#-*-coding: utf-8-*-# author: Hongwei import tkinter as tk # needs to import # step 1, instantiate object, create window windowwindow = tk.Tk () # step 2, name the window window.title ('My Window') # step 3 Set the size of the window (length * width) window.geometry ('500x300') # the multiplication here is small x # step 4, create a tag on the graphical interface to display the content and place the tk.Label (window, text='on the window', bg='red', font= (' Arial', 16)). Pack () # is different from the previous parts to create and place separately, you can actually create and place one step to complete # step 5 Create a main frame, grow on the main window window frame = tk.Frame (window) frame.pack () # step 6, create a second layer frame frame, grow on the main frame frame frame_l = tk.Frame (frame) # layer 2 frame, left frame, grow on the main frame frame_r = tk.Frame (frame) # layer 2 frame, right frame, grow on the main frame frame_l.pack (side='left') frame_r.pack (side='right') # step 7 Create three groups of tags for the content above the second layer frame, divided into left and right areas Identify tk.Label (frame_l, text='on the frame_l1', bg='green'). Pack () tk.Label (frame_l, text='on the frame_l2', bg='green'). Pack () tk.Label (frame_l, text='on the frame_l3', bg='green'). Pack () tk.Label (frame_r, text='on the frame_r1', bg='yellow'). Pack () tk.Label (frame_r, text='on the frame_r2') Bg='yellow'). Pack () tk.Label (frame_r, text='on the frame_r3', bg='yellow'). Pack () # step 8 The main window loops through window.mainloop ()
About margin
The outer margin of the padx,pady is horizontal and vertical.
Ipadx,ipady inside margin.
Padx=10, indicating that the left and right margins are 10 pixels.
Padx= (10 and 20) means the left margin is 10 and the right margin is 20.
Https://www.cnblogs.com/rainbow-tan/p/14694211.html
5.messagebox message box
Import tkinter as tkfrom tkinter import messageboxif _ _ name__ = ='_ main__': def LoginButton (): U = rt.username.get () p = rt.password.get () if len (u) = 0 or len (p) = 0: messagebox.showerror ('prompt:', 'input information is empty') else: messagebox.showinfo ('prompt:' 'username:% s\ npassword:% s\ n'% (u, p)) # main window rt = tk.Tk () rt.geometry (' 300x300') # variable rt.username = tk.StringVar () rt.password = tk.StringVar () # account F1 = tk.Frame (rt) tk.Label (F1, text=' account:') .grid (row=0, column=0 Padx=30) tk.Entry (F1, textvariable=rt.username) .grid (row=0, column=1) f1.grid (pady=50) # password f2 = tk.Frame (rt) tk.Label (f2, text=' password:') .grid (row=1, column=0, padx=30) tk.Entry (f2, show='*', textvariable=rt.password) .grid (row=1, column=1) f2.grid () # login button tk.Button (rt, text=' login' Command=LoginButton) .grid (pady=30) rt.mainloop ()
6. Drop-down box
Def GetIdentity (* args): identity = rt.identity.get () print (identity) # identity drop-down box f0 = tk.Frame (rt) tk.Label (f0, text=' identity:') .grid (row=0, column=0, padx= (5d30)) identityBox = ttk.Combobox (f0, textvariable=rt.identity, values= ['administrator', 'user', 'other'], width=10) identityBox.grid (row=0) Column=1) identityBox.current (1) identityBox.bind (", GetIdentity) f0.grid (padx=0,pady=20) 7.Menu menu
To create a top-level menu, you need to create a menu instance first, and then use the add () method to add commands and other submenus:
Import tkinter as tkroot = tk.Tk () def callback (): print ("~ called ~") # create a top-level menu menubar = tk.Menu (root) menubar.add_command (label = "Hello", command = callback) menubar.add_command (label = "Quit", command = root.quit) # display menu root.config (menu = menubar) root.mainloop ()
two。 Create a drop-down menu (or other submenu) in much the same way, except that they eventually need to be added to the main menu (rather than the window):
Import tkinter as tkroot = tk.Tk () def callback (): print ("~ called ~") # create a top-level menu menubar = tk.Menu (root) # create a drop-down menu "File" Then add it to the top-level menu filemenu = tk.Menu (menubar, tearoff=False) filemenu.add_command (label= "Open", command=callback) filemenu.add_command (label= "Save", command=callback) filemenu.add_separator () filemenu.add_command (label= "exit", command=root.quit) menubar.add_cascade (label= "File", menu=filemenu) # create another drop-down menu "Edit" Then add it to the top-level menu editmenu = tk.Menu (menubar, tearoff=False) editmenu.add_command (label= "cut", command=callback) editmenu.add_command (label= "copy", command=callback) editmenu.add_command (label= "paste", command=callback) menubar.add_cascade (label= "edit", menu=editmenu) # display menu root.config (menu=menubar) root.mainloop ()
Https://blog.csdn.net/qq_41556318/article/details/85273584
Project learning, 1. Roll call machine
a. Navigation bar
The top-level menu is divided into two submenus: the menu and the list of imported names.
The submenu has three functions: delete the library, exit, and show the author.
b. Main body
Use a Label to indicate the prompt (please import the list of names, please select the number of people)
Then the three Label displays how many people you choose and three Button buttons.
The clear zero button is used to clear the three Label.
c. Logic
To run the program, first check whether there is a file library under the current working directory. Txt (person name data table)
If you are not prompted to import, then click Import, which will generate a library .txt in the working directory.
The generate button corresponding to the point is generated from the person table using the random function.
At this point, I believe you have a deeper understanding of "how to understand Python GUI programming". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.