问题描述
我在我的代码中使用了多个按钮,并且它们相互交错.我要他们肩并肩我在这里检查过:在 Python 中设置按钮的位置? 并尝试过该问题的解决方案,
I am using multiple buttons in my code and they come among each other.I want them to be side by side,I have checked here: Setting the position on a button in Python? and have try'd the solution of that question,
但是,我使用 .pack 并且需要将其更改为网格?这就是我所拥有的:
However, I use .pack and I need to change it to grid?This is what I've got:
#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) 将东西并排放置边使用包.
Use pack(side=LEFT) or pack(side=RIGHT) to place things side by side using pack.
然而,总的来说,如果你正在做这个项目之外的任何事情,那么学习使用 grid() 绝对是个好主意.它有点复杂,但无需太多工作即可提供更大的放置灵活性.可以在 本网站上找到有关如何使用每种方法以及它们之间差异的很好参考.
In general however, if you are doing anything beyond this project it is definitely a good idea to learn to use grid(). It's a little more complicated but provides much more flexibility for placement without much work. A good reference on how to use each and the differences between them can be found at this site.
更新
如果您仍然停留在 pack() 上,那么将事情集中在同一条线上会有点棘手.没有直接的方法让它工作,最简单的解决方法是创建一些东西来填充侧面的空间,这样按钮就会被强制放在中间.这样的事情应该可以解决问题:
Getting things centered on the same line is a little more tricky if you are still stuck on pack(). There is no direct way to make it work, the easiest workaround is to create something to fill the space on the side so the buttons are forced to the middle. Something like this should do the trick:
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)
请注意,您必须通过反复试验找出正确的空格数,以使按钮显示为居中.
Note that you will have to figure out the correct number of spaces to make the buttons appear centered through trial and error.
这篇关于Python Tkinter 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!