来自 guizero PushButton.py 的 guizero / tkinter PushButton NameError
guizero / tkinter PushButton NameError from guizero PushButton.py
我是一个 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 版本,因此仍然存在错误。犯下这个错误的具体承诺是:
https://github.com/lawsie/guizero/commit/236064b8781c298a87954775daed65cb384d04f4 (actually, there wasn't any mistakes made there, it's actually when lawsie merged the pull-request).As you can see here,那个人忘记导入 tkinter.DISABLE
和 tkinter.ENABLE
。
您可以在此处报告此问题:https://github.com/lawsie/guizero/issues 希望他们能尽快更改它,因为这是一个非常容易解决的问题。
使用以下方法绕过它:
create_story["state"] = "disabled" 等等
我是一个 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 版本,因此仍然存在错误。犯下这个错误的具体承诺是:
https://github.com/lawsie/guizero/commit/236064b8781c298a87954775daed65cb384d04f4 (actually, there wasn't any mistakes made there, it's actually when lawsie merged the pull-request).As you can see here,那个人忘记导入 tkinter.DISABLE
和 tkinter.ENABLE
。
您可以在此处报告此问题:https://github.com/lawsie/guizero/issues 希望他们能尽快更改它,因为这是一个非常容易解决的问题。
使用以下方法绕过它: create_story["state"] = "disabled" 等等