A few simple GUI interfaces implemented with Tkinter
The way to call Tkinter is simple, just a few lines of code
1 Import Tkinter 2 top = tkinter.tk ()3# Code to add widgets'll go here ... 4 Top.mainloop ()
Using the button
ImportTkinterImportTkmessagebox Top=tkinter.tk ()#Add a function defHello (): Tkmessagebox.showinfo ("title","Hello python from S") #Add a ButtonOK = Tkinter.button (top,bg='Red', text='Hello python', command=hello); #Define the layout and show itOk.pack (); Top.mainloop ()
Several layout methods
Now it seems that the pack's bottom and right are not working, and there may be some special restrictions.
ImportTkinterImportTkmessagebox fromTkinterImport*Top=Tk () frame=Frame (top) frame.pack () BottomFrame=frame (frame) bottomframe.pack (side=BOTTOM) OK= Tkinter.button (bottomframe,bg='Red', text='Red'); Ok.pack (Fill='x', side=TOP); Green= Tkinter.button (bottomframe,bg='Green', text='Green'); Green.pack (Side=Left ); Blue= Tkinter.button (bottomframe,bg='Blue', text='Blue'); Blue.pack (Side=Left ); Brown= Tkinter.button (bottomframe,bg='Brown', text='Brown'); Brown.pack (Side=BOTTOM); Top.mainloop ()
As follows
Place layout
1From TkinterImport *2ImportTkmessagebox3ImportTkinter456 top =tkinter.tk ()78DefHellocallback ():9 Tkmessagebox.showinfo ( "Hello Python", "Hello World"), B = Tkinter.button (Top, Text ="Hello", command = hellocallback) b.pack ()b.place ( Bordermode=outside, height=100, width=100,relx=0.1)top.mainloop ()
Grid layout
ImportTkinterImportTkmessagebox fromTkinterImport*Top=Tk () frame=Frame (top) frame.pack () BottomFrame=frame (frame) bottomframe.pack (side=BOTTOM) OK= Tkinter.button (bottomframe,bg='Red', text='Red'); Ok.grid (Row=1,column=1); Green= Tkinter.button (bottomframe,bg='Green', text='Green'); Green.grid (Row=1,column=2); Blue= Tkinter.button (bottomframe,bg='Blue', text='Blue'); Blue.grid (Row=2,column=1); Brown= Tkinter.button (bottomframe,bg='Brown', text='Brown'); Brown.grid (Row=2,column=2); Top.mainloop ()
Use of Menus
From TkinterImport *DefDonothing (): Filewin =TopLevel (root) button = button (Filewin, text="Do nothing button") Button.pack ()Print‘‘Root =Tk () menubar =Menu (Root)#Add Filemane Filemenu = Menu (menubar, tearoff=0) Filemenu.add_command (label="New", command=donothing) Filemenu.add_command (label="Open", command=donothing) Filemenu.add_command (label="Save", command=donothing) Filemenu.add_command (label="Save as ...", command=donothing) Filemenu.add_command (label="Close", command=donothing) Filemenu.add_separator () Filemenu.add_command (label="Exit", command=Root.quit) Menubar.add_cascade (label="File", menu=Filemenu)#Editmenu Editmenu = Menu (menubar, tearoff=0) Editmenu.add_command (label="Undo", command=donothing) Editmenu.add_separator () Editmenu.add_command (label="Cut", command=donothing) Editmenu.add_command (label="Copydonothing) Editmenu.add_command (label=< Span style= "color: #800000;" > "paste" Command=delete ", Command=donothing) Editmenu.add_command (Label=" Span style= "color: #800000;" >select all "editeditmenu) root.config (Menu=menubar) Root.mainloop ()
Advanced applications such as generating tables and other operations follow-up research
The overall use of the Tkinter is still very simple, but also suitable for some functional integration. Simple and easy to use, Python will not be a good helper to skip the tedious technical learning ideas. But there may be problems with efficiency, such as large-scale presentation of data.
Learning process can be found that there are many advanced applications can go deep learning, such as how the pack layout to do better, layout and multi-threaded collection, if you extend a table project, if and tkinter stripped into a common program and so on.
At the same time see will introduce, said is python in machine learning status is important, a language appears in addition to performance, civilian level is also an important premise of his promotion. Many people who know the algorithm may not like to engage in a large-scale Java project, and it takes a lot of effort to do engineering.
Python can actually open another window outside of the project.
Python Basics-a simple GUI interface