【发布时间】:2015-11-02 19:56:53
【问题描述】:
所以我开始制作生存游戏,但很快就遇到了问题。我想做一个按钮,它应该会收集一定的时间,(如灌木),当按下时,屏幕上显示的灌木数量将增加 15。但每次我尝试制作它时,数量灌木从 0 到 15,这很好,但不会再高了。这是我目前的代码:
import tkinter as tk
root = tk.Tk() # have root be the main window
root.geometry("550x300") # size for window
root.title("SURVIVE") # window title
shrubcount = 0
def collectshrub():
global shrubcount
shrubcount += 15
shrub.config(text=str(shrubcount))
def shrub():
shrub = tk.Label(root, text='Shrub: {}'.format(shrubcount),
font=("Times New Roman", 16)).place(x=0, y=0)
def shrubbutton():
shrubbutton = tk.Button(root, command=collectshrub, text="Collect shrub",
font=('Times New Roman', 16)).place(x=0, y=200)
shrub() # call shrub to be shown
root.mainloop() # start the root window
任何帮助都会很好!谢谢吨 收到这些错误
shrub.config(text=str(shrub count))
AttributeError: 'NoneType' object has no attribute 'config'
【问题讨论】:
-
能否请您删除所有不必要的代码,因为您的问题实际上是在底部,这使得它更难找到。
-
@Inkblot 没问题
-
如果您也马上要致电
place(),则无需致电pack()。任选其一。 -
除非我误解了这一点,否则它从 0 开始,然后当他们单击按钮时添加 15。在第二次单击后,它会回到零,然后加 15。每次像
newshrubcount = shrubcount + shrubcollected之类的东西时,将 15 添加到shrubcount会更容易,并记得声明shrubcollected全局。
标签: python python-3.x tkinter increment