如何在 Tkinter 中将两个按钮并排放置?
Tkinter 通常提供了三种定义窗口小部件几何图形的一般方法。它们是:位置、填充和网格管理。如果我们使用填充几何管理器,然后使用side 属性在框架中放置两个按钮。它将按钮水平叠加在窗口的(左、右、上和下)方向。side 属性在应用程序中所有相邻窗口小部件间保持相同宽度和内部填充。
示例
#Import the required Libraries
from tkinter import *
from tkinter import ttk
import random
#Create an instance of Tkinter frame
win = Tk()
#Set the geometry of Tkinter frame
win.geometry("750x250")
def clear():
entry.delete(0,END)
def display_num():
for i in range(1):
entry.insert(0, random.randint(5,20))
#Define an Entry widget
entry= Entry(win, width= 40)
entry.pack()
#Create Buttons with proper position
button1= ttk.Button(win, text= "Print", command=display_num)
button1.pack(side= TOP)
button2= ttk.Button(win, text= "Clear", command= clear)
button2.pack(side=TOP)
win.mainloop()输出
运行以上代码会显示一个包含两个按钮的窗口,按钮水平叠加并彼此相邻。
现在,单击每个按钮查看结果输出。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP