程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

On Tkinter of Python visual programming (2)

編輯:Python

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 .

One 、 Location of each control

1、Label Location of 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

2、entry Location of control

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()

3、button Location of control

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()

Two 、 Where the window is displayed

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved