Python Tkinter 按钮
Python Tkinter buttons
我在我的代码中使用了多个按钮,它们相互组合在一起。
我要他们肩并肩,
我在这里检查过:Setting the position on a button in Python? 并尝试了该问题的解决方案,
但是,我用的是.pack,需要改成grid吗?
这就是我得到的:
#import tKinter
import sys
from tkinter import *
def mhello():
mlabel1 = Label(window,text = 'Hello we have to start / да започне').pack()
def mhello1():
mlabel2 = Label(window,text = 'Hello we have to stop / да спре').pack()
def mhello2():
mlabel3 = Label(window,text = 'Hello we have to add / да добавите').pack()
#create new window
window = Tk()
#set window title
window.title("Изпитване програмата")
#set window size
window.geometry("700x200")
#menu start
def donothing():
filewin = Toplevel(window)
print("Welcome to my test program, XOXO Katherina")
mlabel5 = Label(window,text = 'Welcome to my test program, XOXO Katherina').pack()
def leave():
sys.exit(0)
menubar = Menu(window)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command=window.destroy)
menubar.add_cascade(label="File", menu=filemenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)
window.config(menu=menubar)
#menu ends
#buttons here
mlabel = Label(window,text = 'My label').pack()
mbutton = Button(window,text = 'Start',command = mhello, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3').pack()
mbutton1 = Button(window,text = 'Stop',command = mhello1, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3').pack()
mbutton2 = Button(window,text = 'Add',command = mhello2, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3').pack()
#end buttons
#draw the window start application
window.mainloop()
也曾尝试将按钮更改为:
#buttons here
mlabel = Label(window,text = 'My label').pack()
mbutton = Button(window,text = 'Start',command = mhello, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3')
mbutton.grid(row=0, column=0)
mbutton1 = Button(window,text = 'Stop',command = mhello1, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3')
mbutton1.grid(row=1, column=0)
mbutton2 = Button(window,text = 'Add',command = mhello2, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3')
mbutton2.grid(row=2, column=0)
#end buttons
使用 pack(side=LEFT) 或 pack(side=RIGHT) 使用 pack 并排放置东西。
但总的来说,如果您正在做本项目以外的任何事情,那么学习使用 grid() 绝对是个好主意。它有点复杂,但无需太多工作即可为放置提供更大的灵活性。在 this site.
可以找到关于如何使用它们以及它们之间的区别的很好的参考
更新
如果您仍然停留在 pack() 上,那么让事情集中在同一条线上会有点棘手。没有直接的方法让它工作,最简单的解决方法是创建一些东西来填充侧面的 space,这样按钮就会被强制到中间。这样的事情应该可以解决问题:
spacing1 = Label(root, text=" "*50).pack(side=LEFT)
button1 = Button(...)
button1.pack(side=Left)
spacing2 = Label(root, text=" "*50).pack(side=RIGHT)
button2 = Button(...)
button2.pack(side=RIGHT)
请注意,您必须通过反复试验找出正确的 space 数量,以使按钮居中显示。
我在我的代码中使用了多个按钮,它们相互组合在一起。 我要他们肩并肩, 我在这里检查过:Setting the position on a button in Python? 并尝试了该问题的解决方案,
但是,我用的是.pack,需要改成grid吗? 这就是我得到的:
#import tKinter
import sys
from tkinter import *
def mhello():
mlabel1 = Label(window,text = 'Hello we have to start / да започне').pack()
def mhello1():
mlabel2 = Label(window,text = 'Hello we have to stop / да спре').pack()
def mhello2():
mlabel3 = Label(window,text = 'Hello we have to add / да добавите').pack()
#create new window
window = Tk()
#set window title
window.title("Изпитване програмата")
#set window size
window.geometry("700x200")
#menu start
def donothing():
filewin = Toplevel(window)
print("Welcome to my test program, XOXO Katherina")
mlabel5 = Label(window,text = 'Welcome to my test program, XOXO Katherina').pack()
def leave():
sys.exit(0)
menubar = Menu(window)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command=window.destroy)
menubar.add_cascade(label="File", menu=filemenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)
window.config(menu=menubar)
#menu ends
#buttons here
mlabel = Label(window,text = 'My label').pack()
mbutton = Button(window,text = 'Start',command = mhello, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3').pack()
mbutton1 = Button(window,text = 'Stop',command = mhello1, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3').pack()
mbutton2 = Button(window,text = 'Add',command = mhello2, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3').pack()
#end buttons
#draw the window start application
window.mainloop()
也曾尝试将按钮更改为:
#buttons here
mlabel = Label(window,text = 'My label').pack()
mbutton = Button(window,text = 'Start',command = mhello, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3')
mbutton.grid(row=0, column=0)
mbutton1 = Button(window,text = 'Stop',command = mhello1, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3')
mbutton1.grid(row=1, column=0)
mbutton2 = Button(window,text = 'Add',command = mhello2, fg = 'black',bg = 'red', height = '3',width = '5',bd = '3')
mbutton2.grid(row=2, column=0)
#end buttons
使用 pack(side=LEFT) 或 pack(side=RIGHT) 使用 pack 并排放置东西。
但总的来说,如果您正在做本项目以外的任何事情,那么学习使用 grid() 绝对是个好主意。它有点复杂,但无需太多工作即可为放置提供更大的灵活性。在 this site.
可以找到关于如何使用它们以及它们之间的区别的很好的参考更新
如果您仍然停留在 pack() 上,那么让事情集中在同一条线上会有点棘手。没有直接的方法让它工作,最简单的解决方法是创建一些东西来填充侧面的 space,这样按钮就会被强制到中间。这样的事情应该可以解决问题:
spacing1 = Label(root, text=" "*50).pack(side=LEFT)
button1 = Button(...)
button1.pack(side=Left)
spacing2 = Label(root, text=" "*50).pack(side=RIGHT)
button2 = Button(...)
button2.pack(side=RIGHT)
请注意,您必须通过反复试验找出正确的 space 数量,以使按钮居中显示。