我是一个Python noob试图结合我通过教程学到的一些东西;专门用guizero制作GUI。
我创建了一个名为player_name的TextBox对象和一个名为create_story的PushButton对象。我的目标是在文本框为空时禁用按钮,并在框中输入内容时启用按钮。
guizero文档列表" enable()"和"禁用()"作为附加到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
答案 0 :(得分:1)
恭喜,您发现了一个错误。正如包裹所述:
这是预发布版本,因此可能存在错误和功能 变化
由于guizero仍处于alpha版本,因此仍然存在漏洞。犯这个错误的具体承诺是:
https://github.com/lawsie/guizero/commit/236064b8781c298a87954775daed65cb384d04f4(实际上,那里没有任何错误,实际上当诉讼merged提出拉动请求时)。正如您可以看到here那样的人忘了导入tkinter.DISABLE
和tkinter.ENABLE
。
您可以在此处报告此问题:https://github.com/lawsie/guizero/issues希望他们尽快更改,因为这是一个非常简单的解决方法。
答案 1 :(得分:0)
使用以下方法解决问题: create_story [“state”] =“禁用”等