我正在研究用于Python 2.7.9(Cookie-Clicker风格游戏)的Tkinter增量游戏,我遇到了网格问题。
我希望Tkinter窗口有三列:第一列用于自动货币生成器,第二列用于主要点击器,第三列用于升级。最上面的一行是一个例外:我想让最上面一行显示左边的玩家余额(即他们有多少钱)和右边的MPS(每秒钱数)。
我在http://effbot.org/tkinterbook/grid.htm处遵循了指南的所有内容,但我尝试过的代码都没有工作(所有小部件都停留在一列中)!
以下是代码:
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()
如果您需要完整代码,请输入:https://github.com/DerpfacePython/Click-Incremental-Game1/blob/master/game.py
P.S。我确实检查了其他问题,但没有一个真正适用于手头的问题(我使用了'类似问题'标签,因为我不知道输入什么来解决问题。)
任何帮助将不胜感激!
答案 0 :(得分:2)
如评论中所述,您无法同时使用grid
和pack
(使用相同的父级)。因此,如果您需要行和列,请使用grid
并删除pack
。同时在grid
- 方法中指定列。
''' 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")