如何定位 Tkinter 窗口上的按钮?
要设置按钮的位置,我们使用按钮控件的方法 place。方法 place 采用按钮的 x 和 y 坐标。
步骤 -
导入所需的库并创建 tkinter 框架实例。
使用 win.geometry 方法设置框架的大小。
接下来,创建多个按钮并将其命名为“Button-1”、“Button-2”等。
通过提供 x 和 y 坐标值,使用 place 方法设置按钮的位置。
最后,运行应用程序窗口的 mainloop。
示例
# Import the Tkinter library
from tkinter import *
from tkinter import ttk
# Create an instance of Tkinter frame
win = Tk()
# Define the geometry
win.geometry("750x350")
# Create Buttons in the frame
button = ttk.Button(win, text="Button-1")
button.place(x=325, y=125)
button = ttk.Button(win, text="Button-2")
button.place(x=325, y=175)
button = ttk.Button(win, text="Button-3")
button.place(x=325, y=225)
#Create a Label
Label(win, text="Position the Buttons", font='Consolas 15').pack()
win.mainloop()输出
当你执行这段代码时,它将显示以下窗口 -
请注意,我们在所有三个按钮中都将 x 变量固定为 325,这就是按钮对其的原因。你可以更改 place 方法中的 (x, y) 值来改变按钮的位置。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP