【问题标题】:Trouble incrementing variables in Python 3.4.3 Tkinter with labels and buttons在 Python 3.4.3 Tkinter 中使用标签和按钮增加变量时遇到问题
【发布时间】: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


【解决方案1】:

在您当前的代码中,您在一个函数中定义了灌木,并在仅在本地分配它时尝试在另一个函数中使用它。解决方案是像这样完全删除shrub()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()  

在看到问题底部的错误后,您的变量 shrubcount 在函数中被命名为 shrub count。像这样的小事情可以完全改变你的代码的工作方式。

【讨论】:

  • 另一个快速的问题,对于 shrub.config 语句中的灌木丛,这就是它的原因,所以它会增加,我该如何回溯那句话,例如 If shrubcount >= 30: print("you collected 30 sticks")我想指的是递增的灌木计数,而不是 0 的全局值
  • @SpencerG555 已添加到我的帖子的答案中。
  • 我的意思是在方法之外。在我的代码的更下方,我有它,如果你有 30 个灌木,那么会出现一个新按钮,让你制作一层灌木睡觉。我知道如何让按钮出现,但我只是不知道如何引用该方法中实际递增的灌木计数
  • 因此,如果您收集了 30 根棍子,您希望出现一个新按钮以及一个新标签,说明您已收集 30 根棍子?
  • 是的,它不会说你已经收集了这些,它只是一个新按钮,可以让你制作灌木床,所以如果你有 30 个灌木,比如 30 个坚持你可以铺床,但我只是不确定如何调用那个递增的变量而不是全局变量
【解决方案2】:

Inkblot 有正确的想法。

您的灌木函数将灌木的数量设置为 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()

注意 add_shrub 所做的只是将灌木计数增加 15。显示灌木的数量由灌木标签对象处理。

【讨论】:

  • 行不通。一直只给我 0,因为当它运行时它可能会更改 add_shrub 中的灌木计数,但它不会更改全局变量,因此屏幕上的灌木计数将保持为 0
  • 使用shrub.config(text=str(shrubcount))每次更新add_shrub下的标签
  • @MobiusCode 我认为这可能与使用全局变量有关,不确定但我尝试过的一切都有效。只是不知道如何用这样的方式永久更改全局变量
  • @Inkblot 这样做时,我得到了错误UnboundLocalError: local variable 'shrubcount' referenced before assignment,所以我将global shrubcount 放入add_shrub,当我这样做时,我现在得到了这个错误:shrub.config(text=str(shrubcount)) AttributeError: 'NoneType' object has no attribute 'config'
  • @SpencerG555 请将其添加到您的问题中,以便我更清楚地看到它
猜你喜欢
  • 1970-01-01
  • 2015-11-15
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
  • 2020-06-23
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多