我是Python,试图结合我通过教程学到的一些东西,特别是用guizero制作一个GUI。
我创建了一个名为TextBox的player_name对象和一个名为create_story的PushButton对象。我的目标是在文本框为空时禁用该按钮,并在输入到该框中时启用该按钮。
guizero文档将“启用()”和“禁用()”作为附加到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发布于 2017-05-28 04:09:25
恭喜你,你发现了一个窃听器。如一揽子计划所述:
这是一个预发布版本,所以可能会有bug和特性可能会改变。
由于guizero仍然处于alpha版本,所以仍然存在bug。做出这一错误的具体承诺是:https://github.com/lawsie/guizero/commit/236064b8781c298a87954775daed65cb384d04f4 (实际上,没有犯任何错误,实际上是在lawsie 合并的拉请求时).As,你可以看到这里,那个人忘了导入tkinter.DISABLE和tkinter.ENABLE。
您可以在这里报告这个问题:希望https://github.com/lawsie/guizero/issues很快就会改变它,因为这是一个非常容易的解决方法。
发布于 2017-05-29 14:35:06
使用以下方式绕过它: create_story"state“=”禁用“等
https://stackoverflow.com/questions/44223606
复制相似问题