In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what is the interface design and implementation of the main form of Python GUI". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. basic interface design
Let's create a new 900x640 window, add a picture at the top, create two Panedwindow containers in the body below, add buttons on the left, and use the right as the TreeView display interface.
From tkinter import * from tkinter.ttk import * import osclass MainWindow (Tk): def _ _ init__ (self): super (). _ init__ () self.title ("main form") self.geometry ("900x640+180+80") self.resizable (0P0) self ["bg"] = "skyblue" # load gui self.setup_UI () def setup_UI ( Self): # set Style self.Style01 = Style () self.Style01.configure ("left.TPanedwindow" Background= "navy") self.Style01.configure ("right.TPanedwindow", background= "skyblue") self.Style01.configure ("TButton", width = 10 bold font = ("Chinese bold", 15, "bold") # Top_banner self.Login_image = PhotoImage (file = "." + os.sep+ "img" + os.sep+ "stu_main_top_banner.png") self.Lable_image = Label (self Image = self.Login_image) self.Lable_image.pack () # left: button area, create a container self.Pane_left = PanedWindow (width= 200 height=540,style height = 540 self.Pane_left.place = "left.TPanedwindow") self.Pane_left.place (x = 4 Magi y = 94) self.Pane_right = PanedWindow (width=685, height=540,style = "right.TPanedwindow") self.Pane_right.place (x = 210 Y = 94) # add the left button self.Button_add = Button (self.Pane_left,text = "add student", style = "TButton") self.Button_add.place (x = 40, y = 20) self.Button_update = Button (self.Pane_left,text = "modify student",) self.Button_update.place (x = 40, y = 45) self.Button_delete = Button (self.Pane_left Text= "Delete students",) self.Button_delete.place (xdelete 40, yearly 70) self.Button_modify = Button (self.Pane_left, text= "change password",) self.Button_modify.place (xdelete 40, yearly 120) # right: query, TreeViewif _ _ name__ = ='_ _ main__': this_main = MainWindow () this_main.mainloop ()
Second, add query area
In the Pannedwindow container on the right, add a LabelFrame container as the query area, and add a series of Label, Entry, Button controls to the LabelFrame container. You can enter the student number, name, phone number, ID card, query, and display all the information:
Self.Pane_right = PanedWindow (width=725, height=540,) self.Pane_right.place (x = 170, y = 94) # LabelFrameself.LabelFrame_query = LabelFrame (self.Pane_right,text = "student information query", width= 700) self.LabelFrame_query.place (x = 10, y = 10) # add control self.Label_sno = Label (self.LabelFrame_query,text = "student ID:") self.Label_sno.place (x = 5) Yidong 13) self.Entry_sno = Entry (self.LabelFrame_query,width = 8) self.Entry_sno.place (x = 40 Magi y = 10) self.Label_name = Label (self.LabelFrame_query, text= "name:") self.Label_name.place (self.LabelFrame_query 125,13) self.Entry_name = Entry (self.LabelFrame_query,width = 8) self.Entry_name.place (Xerox 160,10) self.Label_mobile = Label (self.LabelFrame_query, text= "Tel:") self.Label_mobile.place (Xerox 245 Yee 13) self.Entry_mobile = Entry (self.LabelFrame_query, width=8) self.Entry_mobile.place (x = 280,10) self.Label_id = Label (self.LabelFrame_query, text= "ID:") self.Label_id.place (x = self.LabelFrame_query 365, y = 13) self.Entry_id = Entry (self.LabelFrame_query, width=10) self.Entry_id.place (x x = 420, y = 10) self.Button_query = Button (self.LabelFrame_query, text= "query", width= 4) self.Button_query.place (x = 520) 10) self.Button_all = Button (self.LabelFrame_query, text= "Show all", width = 8) self.Button_all.place (x = 590, y = 10) display effect:
3. Load Treeview control
Create controls, set alignment, and header for each column
# add TreeView controls self.Tree = Treeview (self.Pane_right,columns= ("sno", "names", "gender", "birthday", "mobile", "email", "address"), show= "headings", height=20) # set the width and alignment of each column self.Tree.column ("sno", width=100,anchor= "center") self.Tree.column ("names", width=80,anchor= "center") self.Tree.column ("gender", width=80) Anchor= "center") self.Tree.column ("birthday", width=100,anchor= "center") self.Tree.column ("mobile", width=100,anchor= "center") self.Tree.column ("email", width=100,anchor= "center") self.Tree.column ("address", width=120,anchor= "center") # set the title self.Tree.heading ("sno", text= "student number") self.Tree.heading ("names", text= "name") self.Tree.heading ("gender") for each column Text= "gender") self.Tree.heading ("birthday", text= "birthday") self.Tree.heading ("mobile", text= "mobile number") self.Tree.heading ("email", text= "email address") self.Tree.heading ("address", text= "home address") self.Tree.place
Fourth, realize loading login information of login users.
After the login is successful, the user name and login time are displayed at the top. How did the user name come from? We typed it in the login window, so this involves the transfer of data across forms. This is very important! Login form (login information) = > the basic way of transmission of the main form: the constructor adds a receive parameter current_user in the constructor of the main form, which passes the parameters in when the login form loads the new form; but the variable user of the user name in the login function login () of the login form is a local variable, and there is no variable after the function call, so how do you call it? We need to define global variables in the constructor of the login form:
Self.user = "" # current user
To get the time when the user is logged in, we define a method to get the current time:
Def get_now_time (self): today = datetime.today () return ("d-d-d d:d:d"% (today.year, today.month,today.day,today.hour,today.minute,today.second))
Then pass the parameters self.user and self.get_now_time () as parameters when the main form is loaded
Main_window = maingui.MainWindow (self.user,self.get_now_time ())
On the other hand, we add global variables in the constructor in the main form
Self.login_user = current_userself.login_time = current_time
After that, we display the user information through tags in Top_banner:
Self.Label_login_user = Label (self,text = "current user:" + str (self.login_user). Title () + "\ nLogin time:" + self.login_time) self.Label_login_user.place (x = 650Coy = 40)
In this way, the main window will display the user name logged in through the login window (the initials are automatically capitalized) and the login time: effect demonstration:
5. Load student information into TreeView
1. We define global variables in the main form to store student information:
Self.all_student_list = [] self.file_path = "/ Users/yushengtan/Desktop/Demo/Studentmgr/Student.txt"
two。 Define a method to read student information in a file
Def load_file_student_info (self): if not os.path.exists (self.file_path): showinfo ("system message", "the file name provided does not exist!") Else: try: with open (file = self.file_path,mode = "r") as fd: # one line at a time current_line = fd.readline () while current_line: temp_list = current_line.split (" ") # long string split layer three self.all_student_list.append (temp_list) # read the next line, finish the loop and end current_line = fd.readline () except: showinfo (" system message "," file read exception! ")
Then we write this function in the constructor to automatically write the student information to the all_student_list
Self.load_file_student_info ()
3. Define how to load TreeView information
The student information read in the file is stored in the all_student_list list, which is passed into the method of loading TreeView as a parameter
Def load_treeview (self,current_list:list): # determine whether there is data: if len (current_list) = = 0: showinfo ("system message", "No data load") else: for index in range (len (current_list)): self.Tree.insert (", index,values= (current_ [current_list] [0], current_ list [index] [1]) Current_ list [index] [2], current_ list [index] [3], current_ list [4], current_ list [5], current_ list [index] [6]))
This method is called in the constructor to automatically load all student information into TreeView.
The content of self.load_treeview (self.all_student_list) "what is the interface design and implementation of Python GUI main form" is introduced here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.