In the last article, we talked about tkinter The basic part of , The layout is also quite simple . The position is also fixed , But when we actually design , Or when you write your own layout, you want to be free . You can adjust the position of the control at will , Only in this way can you have your own feeling , So this article is also about the medium-level part , How to adjust the position of each control .
Let's make a small change based on the above , See what the effect is
import tkinter
# Create a window object
screen = tkinter.Tk()
# establish label object
label = tkinter.Label(screen,text=" This is a label Control ")
# Show label,pack The function is adaptive
label.pack()
# Use mainloop Method to make the window display
screen.mainloop()
The above code is the original , If you want to design your own position, you need to change label.pack() This place . The specific code is as follows
import tkinter
# Create a window object
screen = tkinter.Tk()
# establish label object
label = tkinter.Label(screen,text=" This is a label Control ")
# Show label,pack The function is adaptive
label.place(x=30, y=50)
# Use mainloop Method to make the window display
screen.mainloop()
The above figure shows the position of the changed label , Two important keyword parameters ,x and y, It's just two coordinates , Abscissa and ordinate , I can adjust it according to the actual situation
and lebel Same operation , The code is as follows , And then look at the picture
import tkinter
# Create a window object
screen = tkinter.Tk()
# establish label object
label = tkinter.Label(screen,text=" This is a label Control ")
# Show label,pack The function is adaptive
label.place(x=30, y=50)
u = tkinter.StringVar()
text = tkinter.Entry(screen, textvariable=u)
text.place(x=120, y=50)
# Use mainloop Method to make the window display
screen.mainloop()
This is also to look at the code first and then the diagram
import tkinter
# Create a window object
screen = tkinter.Tk()
# Set the position and size of the window
# establish label object
label = tkinter.Label(screen, text=" This is a label Control ")
# Show label,pack The function is adaptive
label.place(x=30, y=50)
u = tkinter.StringVar()
text = tkinter.Entry(screen, textvariable=u)
text.place(x=120, y=50)
u.set("hello")
def run():
print(u.get())
# among commend The value of is the function name
button = tkinter.Button(screen, text=" Am I ", command=run)
button.place(x=120, y=70)
# Use mainloop Method to make the window display
screen.mainloop()
When you run the program, the window appears in the upper left corner by default , And it's small , So here's how to set the position and size . Want to use geometry() The function is as follows
import tkinter
# Create a window object
screen = tkinter.Tk()
# Set the position and size of the window
screen.geometry("800x600+300+50")
# establish label object
label = tkinter.Label(screen, text=" This is a label Control ")
# Show label,pack The function is adaptive
label.place(x=30, y=50)
u = tkinter.StringVar()
text = tkinter.Entry(screen, textvariable=u)
text.place(x=120, y=50)
u.set("hello")
def run():
print(u.get())
# among commend The value of is the function name
button = tkinter.Button(screen, text=" Am I ", command=run)
button.place(x=120, y=70)
# Use mainloop Method to make the window display
screen.mainloop()
geometry("800x600+300+50") The parameters inside 800 and 600 Between is XYZ Of X, No *
That's all tkinter The middle order part of , The next one is going to talk about how to add a menu to the window , Make the window look more professional