Python 带网格的Tkinter问题

Python 带网格的Tkinter问题,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我正在为Python2.7.9开发Tkinter增量游戏(Cookie Clicker风格的游戏),我被网格困住了 我希望Tkinter窗口有三列:第一列用于自动货币生成器,第二列用于主点击器,第三列用于升级。最上面一行是个例外:我希望最上面一行的左边是玩家的余额(即他们有多少钱),右边是MPS(每秒的钱) 我遵循了指南中的所有内容,但是我尝试的代码都不起作用(所有的小部件都卡在一列中) 代码如下: checklabel1 = Label(master, textvariable=moneytk

我正在为Python2.7.9开发Tkinter增量游戏(Cookie Clicker风格的游戏),我被网格困住了

我希望Tkinter窗口有三列:第一列用于自动货币生成器,第二列用于主点击器,第三列用于升级。最上面一行是个例外:我希望最上面一行的左边是玩家的余额(即他们有多少钱),右边是MPS(每秒的钱)

我遵循了指南中的所有内容,但是我尝试的代码都不起作用(所有的小部件都卡在一列中)

代码如下:

checklabel1 = Label(master, textvariable=moneytkinter)
checklabel1.grid(row=0, sticky=W)
checklabel1.pack()

mpslabel = Label(master, textvariable=mpstkinter)
mpslabel.grid(row=0, sticky=E)
mpslabel.pack()

clickbutton = Button(master, textvariable=inctkinter, command=collectmoney)
clickbutton.grid(row=1, column=1)
clickbutton.pack()

incbutton1 = Button(master, textvariable=autopricetkinter, command=deduction1)
incbutton1.grid(row=1, sticky=W)
incbutton1.pack()

checklabel2 = Label(master, textvariable=autoclicktkinter)
checklabel2.grid(row=2, sticky=W)
checklabel2.pack()

incbutton2 = Button(master, textvariable=printpricetkinter, command=deduction2)
incbutton2.grid(row=3, sticky=W)
incbutton2.pack()

checklabel3 = Label(master, textvariable=printmoneytkinter)
checklabel3.grid(row=4, sticky=W)
checklabel3.pack()

clickboost1 = Button(master, text="Click Boost (Costs: $2000)", command=clickboost1)
clickboost1.grid(row=1, sticky=E)
clickboost1.pack()

boostbutton1 = Button(master, text="Auto Clicker Boost (Costs: $5000)", command=boostauto1)
boostbutton1.grid(row=2, sticky=E)
boostbutton1.pack()

boostbutton2 = Button(master, text="Money Printer Boost (Costs: $100000) command=boostauto2)
boostbutton2.grid(row=3, sticky=E)
boostbutton2.pack()
如果您需要完整的代码,请参阅:

另外,我确实检查了其他问题,但没有一个真正适用于手头的问题(我使用了“类似问题”选项卡,因为我不知道输入什么来解决问题)


任何帮助都将不胜感激

如评论中所述,您不能同时使用
grid
pack
(与同一个父级一起使用)。因此,如果需要行和列,只需使用
grid
并删除
pack
。还可以在
网格中指定列

''' Column block 0'''
checklabel1 = Label(master, textvariable=moneytkinter)
checklabel1.grid(row=0, column=0, columnspan=3) # columnspan = overlap 3 columns

mpslabel = Label(master, textvariable=mpstkinter)
mpslabel.grid(row=1,  column=0)

clickbutton = Button(master, textvariable=inctkinter, command=collectmoney)
clickbutton.grid(row=2, column=0, sticky="WE")

incbutton1 = Button(master, textvariable=autopricetkinter, command=deduction1)
incbutton1.grid(row=3, column=0, sticky="WE")

''' Column block 1'''
checklabel2 = Label(master, textvariable=autoclicktkinter)
checklabel2.grid(row=1, column=1)

incbutton2 = Button(master, textvariable=printpricetkinter, command=deduction2)
incbutton2.grid(row=2, column=1, sticky="WE")

''' Column block 2'''
checklabel3 = Label(master, textvariable=printmoneytkinter)
checklabel3.grid(row=1, column=2)

clickboost1 = Button(master, text="Click Boost (Costs: $2000)", command=clickboost1)
clickboost1.grid(row=2, column=2, sticky="WE")

boostbutton1 = Button(master, text="Auto Clicker Boost (Costs: $5000)", command=boostauto1)
boostbutton1.grid(row=3, column=2, sticky="WE")

boostbutton2 = Button(master, text="Money Printer Boost (Costs: $100000)", command=boostauto2)
boostbutton2.grid(row=4, column=2, sticky="WE")

你应该具体说明你所说的“不起作用”的确切含义,你得到了什么,你期望得到什么?你不能同时使用
grid()
pack()
。@VRage-Uh。。。你能给我一个答案吗?我真的不知道你的意思。弗拉格是说你要么1)扔掉所有的
pack
命令,要么2)扔掉所有的
grid
命令。但是在当前状态下完全调试代码并不容易。所有
StringVar
的定义在哪里,例如
moneytkinter
,回调函数在哪里?为什么大多数标签和按钮都有多个定义?请更改您的代码,使其成为一个。@PM2Ring我已经放置了一个指向GitHub的链接,其中包含了我的完整代码。只是问一下,如果您同时使用
grid
pack
会发生什么?它是否像我的代码一样,
pack
覆盖了
grid
——或者这只是一个例外?在您的情况下,它覆盖了
grid
,因为您没有使用
grid
pack
。因此,如果您这样做:
button1.pack()
button2.grid()
您的GUI将不会显示,也不会收到任何错误消息。