在Python3.4.3 Tkinter中使用标签和按钮递增变量时遇到问题
所以我开始做一个生存游戏,但很快就遇到了问题。我想制作一个按钮,该按钮将在一定时间内运行并收集数据(如在灌木中),当按下该按钮时,屏幕上显示的灌木数量将增加15。但是每次我试着做的时候,灌木的数量从0增加到15,这很好,但不会再增加。以下是我目前拥有的代码:在Python3.4.3 Tkinter中使用标签和按钮递增变量时遇到问题,python,python-3.x,tkinter,increment,Python,Python 3.x,Tkinter,Increment,所以我开始做一个生存游戏,但很快就遇到了问题。我想制作一个按钮,该按钮将在一定时间内运行并收集数据(如在灌木中),当按下该按钮时,屏幕上显示的灌木数量将增加15。但是每次我试着做的时候,灌木的数量从0增加到15,这很好,但不会再增加。以下是我目前拥有的代码: import tkinter as tk root = tk.Tk() # have root be the main window root.geometry("550x300") # s
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'
墨迹有正确的想法 “灌木”函数将灌木的数量设置为0 单击“收集灌木”调用强制灌木输出灌木计数,但灌木计数变量在任何时候都不会更新 代码的更干净版本,可以满足您的需要,可能如下所示:
import ttk
from Tkinter import *
class Gui(object):
root = Tk()
root.title("Survive")
shrubs = IntVar()
def __init__(self):
frame = ttk.Frame(self.root, padding="3 3 12 12")
frame.grid(column=0, row=0, sticky=(N, W, E, S))
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
ttk.Label(frame, text="Shrubs:").grid(column=0, row=0, sticky=W)
ttk.Entry(frame, textvariable=self.shrubs, width=80).grid(column=0, row=2, columnspan=4, sticky=W)
ttk.Button(frame, command=self.add_shrubs, text="Get Shrubs").grid(column=6, row=3, sticky=W)
def add_shrubs(self):
your_shrubs = self.shrubs.get()
self.shrubs.set(your_shrubs + 15)
go = Gui()
go.root.mainloop()
请注意,“添加灌木”所做的一切都是将灌木计数增加15。灌木数量的显示由灌木标签对象处理。在当前代码中,您在一个函数中定义了灌木,并尝试在另一个函数中使用它,而您只在本地分配了它。解决方案是完全删除
shrubbutton()
和shrubbutton()
函数,如下所示:
import tkinter as tk
root = tk.Tk()
root.geometry("550x300")
root.title("SURVIVE")
shrubcount = 0
def collectshrub():
global shrubcount
shrubcount += 15
shrub.config(text="Shrub: {" + str(shrubcount) + "}")
if shrubcount >= 30:
print(text="You collected 30 sticks")
craftbutton.pack()
def craftbed():
#Do something
shrub = tk.Label(root, text="Shrub: {" + str(shrubcount) + "}", font=("Times New Roman", 16))
shrub.place(x=0, y=0)
shrubbutton = tk.Button(root, command=collectshrub, text="Collect shrub", font=('Times New Roman', 16))
shrubbutton.place(x=0, y=200)
craftbutton = tk.Button(root, text="Craft bed", comma=craftbed)
root.mainloop()
同时
在看到问题底部出现的错误后,函数中将变量
shubbcount
命名为shublcount
。像这样的小事情会完全改变代码的工作方式。你能删除所有不必要的代码吗?因为你的问题实际上在底部,这使得它更难找到。@Inkblot好的,刚刚做了如果你马上也要调用place()
,就不需要调用pack()
。选择一个或另一个。除非我误解了这一点,否则它从0开始,然后当他们单击按钮时,添加15。第二次单击后,它返回到零,然后加15。每次像newshrubcount=shrubcount+shrubcollected
这样的东西时,将15添加到shrubcount
会更容易,并且记住要声明shrubcollected
全局。这不起作用。一直只给我0,因为当它运行时,它可能会更改add_shubsh中的shubcount,但它不会更改全局变量,因此shubbcount在屏幕上保持为0使用shubsh.config(text=str(shubbcount))
每次在add\u
@MobiusCode下更新标签时,我想这可能与使用全局变量有关,不确定,但我尝试过的所有方法都有效。只是不知道如何永久性地改变一个全局变量,比如this@Inkblot这样做时,我得到了错误UnboundLocalError:赋值前引用的局部变量'shubCount',
因此我将global shubbcount
放入add_shub
中,当我这样做时,我现在得到了这个错误:shubsh.config(text=str(灌木计数))AttributeError:'NoneType'对象没有属性'config'
@SpencerG555请将其添加到您的问题中,以便我能更清楚地看到它另一个快速问题,对于Bush.config
语句中的灌木丛out,这是使其增加的一个问题,我如何引用类似于If shrubcount的说法>=30:
打印(“您收集了30根棍子”)
我想指的是增加的灌木数量,而不是增加的全局数量0@SpencerG555在我的帖子的答案中添加。我的意思是在方法之外。在我的代码中,我有它,因此,如果你有30个灌木,那么会出现一个新按钮,让你制作一层灌木睡觉。我知道如何获得屁股但我不知道如何引用灌木丛数量,它实际上是以这种方式递增的。所以,如果你收集了30根树枝,你希望一个新按钮与一个新标签一起出现,标签上写着你收集了30根树枝?是的,它不会说你收集了这些,它只是一个新按钮,可以让你制作一张床如果你有30根灌木和30根树枝,你可以做一张床,但我不确定如何调用递增变量,而不是全局变量