【发布时间】:2015-03-31 13:58:54
【问题描述】:
我在我的代码中使用了多个按钮,它们是相互关联的。 我希望他们并肩, 我在这里检查过:Setting the position on a button in Python? 并尝试了该问题的解决方案,
但是,我使用 .pack 并且需要将其更改为网格? 这就是我所拥有的:
#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分配给同一行。grid也一样。请参阅this post 了解更多信息。