gpt4 book ai didi

python - guizero/tkinter PushButton NameError 来自 guizero PushButton.py

转载 作者:太空宇宙 更新时间:2023-11-03 15:10:41 26 4
gpt4 key购买 nike

我是一名 Python 菜鸟,试图将我通过教程学到的一些东西结合起来;专门用 guizero 制作一个 GUI。

我创建了一个名为 player_name 的 TextBox 对象和一个名为 create_story 的 PushButton 对象。我的目标是在文本框为空时禁用该按钮,并在文本框中输入内容时启用该按钮。

guizero 文档列出了“enable()”和“disable()”作为附加到 PushButton 的方法,但没有详细说明:https://lawsie.github.io/guizero/pushbutton/#methods

到目前为止的完整代码:

import random
from guizero import App, Text, TextBox, PushButton, ButtonGroup, Combo, Box

def print_story():
print("Button Pressed")

app = App(title="Visual Adventure", width=550, height=400,)

hello_message = Text(app, text="Hello, Traveler", size=20, color="red")
story_message = Text(app, text="Would you like to hear a tale?", size=14, color="black")

# organize into box with grid
selections = Box(app, layout="grid")

# text for questions
name_ques = Text(selections, text="What is your name?", size=10, color="black", grid=[0,0], align="left")
gender_ques = Text(selections, text="Boy or a girl?", size=10, color="black", grid=[0,2], align="left")
day_ques = Text(selections, text="What day is it?", size=10, color="black", grid=[0,4], align="left")

# text for grid padding
pad1 = Text(selections, text=" ", size=10, grid=[0,1], align="left")
pad2 = Text(selections, text=" ", size=10, grid=[0,3], align="left")

# interactive objects
player_name = TextBox(selections, width=15, grid=[1,0], align="top")
player_gender = ButtonGroup(selections, options=[ ["Boy", "He"], ["Girl", "She"] ], selected="He", horizontal=True, grid=[1,2], align="top")
day_set = Combo(selections, options=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], selected="Monday", grid=[1,4], align="top")

create_story = PushButton(app, command=print_story, text="Tell me!")

if not player_name.get():
create_story.disable()
elif player_name.get():
create_story.enable()

app.display()

错误:

Traceback (most recent call last):
File "/home/pi/Desktop/python/VisualAdventure.py", line 32, in <module>
create_story.disable()
File "/usr/local/lib/python3.4/dist-packages/guizero/PushButton.py", line 59, in disable
self.config(state=DISABLED)
NameError: name 'DISABLED' is not defined

最佳答案

恭喜您,您发现了一个错误。正如包装上所述:

This is a pre-release version, so there may be bugs and features may change.

因为 guizero 仍处于 alpha 版本,所以仍然有 bug 有待发现。犯这个错误的具体 promise 是: https://github.com/lawsie/guizero/commit/236064b8781c298a87954775daed65cb384d04f4 (实际上,那里没有犯任何错误,实际上是在lawsie merged拉取请求时)。正如你所看到的here ,那个人忘记导入 tkinter.DISABLEtkinter.ENABLE

您可以在此处报告问题:https://github.com/lawsie/guizero/issues希望他们尽快改变它,因为这是一个非常简单的修复。

关于python - guizero/tkinter PushButton NameError 来自 guizero PushButton.py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44223606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com