【发布时间】:2020-04-29 01:01:10
【问题描述】:
我正在 Tkinter 中制作游戏。一个动态的点击游戏,你必须购买你“点击”的东西。我一直在尝试启动库存屏幕,但遇到了一个我无法理解的问题。当玩家通过单击按钮打开“库存”屏幕时,会出现一个窗口,其中包含他们拥有的所有水果(例如,苹果:654(测试编号))。但由于某种原因,什么都没有出现。完全没有。没有错误,也没有标签。这是为什么呢?
代码:
from tkinter import *
money = 0
currentfruit = "Apple"
numOfApples = 654
def clicked():
global money
global currentfruit
if currentfruit == "Apple":
money = money + 1
moneystringvar.set("You have $" + str(money))
if money < 10:
moneylabel.place(x="327", y="0")
if money < 100 and money > 9:
moneylabel.place(x="320", y="0")
if money < 1000 and money > 99:
moneylabel.place(x="315", y="0")
def inventoryOnClose():
root.deiconify()
inventorywindow.destroy()
def marketOnClose():
root.deiconify()
marketwindow.destroy()
def upgradesOnClose():
root.deiconify()
upgradeswindow.destroy()
def inventory():
global inventorywindow
global numOfApples
root.withdraw()
inventorywindow = Tk()
inventorywindow.title("Fruit Clicker - Inventory")
inventorywindow.geometry("400x350+300+100")
inventorywindow.protocol("WM_DELETE_WINDOW", inventoryOnClose)
applesinventory = StringVar()
applesinventory.set("Apples: " + str(numOfApples))
applesinvlabel = Label(inventorywindow, textvariable=applesinventory)
applesinvlabel.place(x="0", y="0")
inventorywindow.mainloop()
def market():
global marketwindow
root.withdraw()
marketwindow = Tk()
marketwindow.title("Fruit Clicker - Market")
marketwindow.geometry("400x350+300+100")
marketwindow.protocol("WM_DELETE_WINDOW", marketOnClose)
marketwindow.mainloop()
def upgrades():
global upgradeswindow
root.withdraw()
upgradeswindow = Tk()
upgradeswindow.title("Fruit Clicker - Upgrades")
upgradeswindow.geometry("400x350+300+100")
upgradeswindow.protocol("WM_DELETE_WINDOW", upgradesOnClose)
upgradeswindow.mainloop()
root = Tk()
root.title("Fruit Clicker")
root.geometry("400x350+300+100")
inventorybutton = Button(root, text="Inventory", fg="White", bg="Black", width="6", command=inventory)
inventorybutton.grid(column="0", row="0")
marketbutton = Button(root, text="Market", fg="White", bg="Black", width="6", command=market)
marketbutton.grid(column="0", row="1")
upgradesbutton = Button(root, text="Upgrades", fg="White", bg="Black", width="6", command=upgrades)
upgradesbutton.grid(column="0", row="2")
leftfruitbutton = Button(root, text="<")
leftfruitbutton.place(x="100", y="285")
currentfruitstringvar = StringVar()
if currentfruit == "Apple":
currentfruitstringvar.set("Apple x" + str(numOfApples))
currentfruitlabel = Label(root, textvariable=currentfruitstringvar, fg="Black")
if numOfApples < 10:
currentfruitlabel.place(x="180", y="290")
if numOfApples < 100 and numOfApples > 9:
currentfruitlabel.place(x="175", y="290")
if numOfApples < 1000 and numOfApples > 99:
currentfruitlabel.place(x="170", y="290")
rightfruitbutton = Button(root, text=">")
rightfruitbutton.place(x="287", y="285")
moneystringvar = StringVar()
moneystringvar.set("You have $" + str(money))
moneylabel = Label(root, textvariable=moneystringvar)
if money < 10:
moneylabel.place(x="327", y="0")
if money < 100 and money > 9:
moneylabel.place(x="320", y="0")
if money < 1000 and money > 99:
moneylabel.place(x="315", y="0")
clickerphoto = PhotoImage(file = "apple.png")
clickerbutton = Button(root, text="Clicker Button", image=clickerphoto, fg="Black", command=clicked)
clickerbutton.place(x="100", y="75")
root.mainloop()
我不确定你是否需要 apple.png,但如果你需要,这里是我的 GitHub 存储库的链接:https://github.com/SeaPuppy2006/FruitClicker
谢谢!
【问题讨论】:
-
只能同时使用一个
Tk()实例,在函数中将Tk()改为Toplevel()。 -
@jizhihaoSAMA 我这样做了,它给了我错误
NameError: name 'TopLevel' is not defined -
仔细看,
Toplevel()不是TopLevel()。 -
啊哈。有效。谢谢
标签: python python-3.x tkinter