Python- 如何在屏幕中间左右并排放置两个按钮?
Python- How to place two buttons left-right and next to each other in the middle of screen?
首先,我阅读了相关讨论:
How can I put 2 buttons next to each other?.
不过,我还是一头雾水。我的代码:
#导入所需的库
from tkinter import *
from tkinter import ttk
import random
win = Tk()
win.geometry("750x250")
def clear():
entry.delete(0,END)
def display_num():
for i in range(1):
entry.insert(0, random.randint(5,20))
entry= Entry(win, width= 40)
entry.pack()
button1= ttk.Button(win, text= "Print", command=display_num)
button1.pack(side= LEFT)
button2= ttk.Button(win, text= "Clear", command= clear)
button2.pack(side=LEFT)
win.mainloop()
现在我得到了
我想要在屏幕中间的条目(白框)正下方有两个按钮。
如何解决?谢谢!
你可以再做一个横排的tk.Frame
在下面打包;例如:
entry= Entry(win, width= 40)
entry.pack()
buttons = ttk.Frame(win)
buttons.pack(pady = 5)
button1= ttk.Button(buttons, text= "Print", command=display_num)
button1.pack(side = LEFT)
button2= ttk.Button(buttons, text= "Clear", command= clear)
button2.pack()
或者您可以使用 grid
layout manager。
entry= Entry(win, width= 40)
entry.grid(row = 0, column = 0, columnspan = 2)
button1= ttk.Button(win, text= "Print", command=display_num)
button1.grid(row = 1, column = 0)
button2= ttk.Button(win, text= "Clear", command= clear)
button2.grid(row = 1, column = 1)
使用包意味着使用包裹,因此在我们进一步讨论时,想象一下您的小部件周围有一个矩形。默认情况下,您的值如下所示:
widget.pack(side='top',expand=False,fill=None,anchor='center')
要让您的小部件出现在您喜欢的位置,您需要通过这些参数对其进行定义。边决定了它应该添加的方向。 Expand 告诉你的 parcel 在 master 中消耗额外的 space。 fill 告诉你的小部件在 x 或 y 或 both 方向伸展。您还可以选择将小部件锚定在 east,west,north,南或其组合。
from tkinter import *
from tkinter import ttk
import random
win = Tk()
win.geometry("750x250")
def clear():
entry.delete(0,END)
def display_num():
for i in range(1):
entry.insert(0, random.randint(5,20))
entry= Entry(win)
entry.pack(fill='x')
button1= ttk.Button(win, text= "Print", command=display_num)
button1.pack(side= LEFT,expand=1,anchor='ne')
button2= ttk.Button(win, text= "Clear", command= clear)
button2.pack(side=LEFT,expand=1,anchor='nw')
win.mainloop()
要了解有关组织小部件和 tkinter 几何管理的更多信息,。
首先,我阅读了相关讨论: How can I put 2 buttons next to each other?.
不过,我还是一头雾水。我的代码:
#导入所需的库
from tkinter import *
from tkinter import ttk
import random
win = Tk()
win.geometry("750x250")
def clear():
entry.delete(0,END)
def display_num():
for i in range(1):
entry.insert(0, random.randint(5,20))
entry= Entry(win, width= 40)
entry.pack()
button1= ttk.Button(win, text= "Print", command=display_num)
button1.pack(side= LEFT)
button2= ttk.Button(win, text= "Clear", command= clear)
button2.pack(side=LEFT)
win.mainloop()
现在我得到了
我想要在屏幕中间的条目(白框)正下方有两个按钮。
如何解决?谢谢!
你可以再做一个横排的tk.Frame
在下面打包;例如:
entry= Entry(win, width= 40)
entry.pack()
buttons = ttk.Frame(win)
buttons.pack(pady = 5)
button1= ttk.Button(buttons, text= "Print", command=display_num)
button1.pack(side = LEFT)
button2= ttk.Button(buttons, text= "Clear", command= clear)
button2.pack()
或者您可以使用 grid
layout manager。
entry= Entry(win, width= 40)
entry.grid(row = 0, column = 0, columnspan = 2)
button1= ttk.Button(win, text= "Print", command=display_num)
button1.grid(row = 1, column = 0)
button2= ttk.Button(win, text= "Clear", command= clear)
button2.grid(row = 1, column = 1)
使用包意味着使用包裹,因此在我们进一步讨论时,想象一下您的小部件周围有一个矩形。默认情况下,您的值如下所示:
widget.pack(side='top',expand=False,fill=None,anchor='center')
要让您的小部件出现在您喜欢的位置,您需要通过这些参数对其进行定义。边决定了它应该添加的方向。 Expand 告诉你的 parcel 在 master 中消耗额外的 space。 fill 告诉你的小部件在 x 或 y 或 both 方向伸展。您还可以选择将小部件锚定在 east,west,north,南或其组合。
from tkinter import *
from tkinter import ttk
import random
win = Tk()
win.geometry("750x250")
def clear():
entry.delete(0,END)
def display_num():
for i in range(1):
entry.insert(0, random.randint(5,20))
entry= Entry(win)
entry.pack(fill='x')
button1= ttk.Button(win, text= "Print", command=display_num)
button1.pack(side= LEFT,expand=1,anchor='ne')
button2= ttk.Button(win, text= "Clear", command= clear)
button2.pack(side=LEFT,expand=1,anchor='nw')
win.mainloop()
要了解有关组织小部件和 tkinter 几何管理的更多信息,