问题描述
我的项目是创建一个GUI表单,从中我将获取密码的长度和密码的数量。然后,我将显示随机密码的数量。
from guizero import App,Text,TextBox,PushButton,error
import random
# char for creating password
_chars = 'abcdefghijklmnopqrstwxyzABCDEFGHIJKLMnopQRSTUVWXYZ1234567890.,!@#$%&'
def _say_my_name():
_welcome_message.value = "Your random value is: " + _length.value
_app = App(title="Password Creator")
message = Text(_app,text="Welcome")
_welcome_message = Text(_app,text="Create your random password here",size=19,font="Arial",color="hotpink")
#TextBox for input of password length
_length_label = Text(_app,text="Give me length of password:",size=11,font="Arial")
_length = TextBox(_app,width=33)
_length_int = int(_length.value) #THIS DOESN'T WORK
#TextBox for input of password qty: 3 pcs/ 4pcs random password
_qty_label = Text(_app,text="How many password you want: ")
_qty_of_password = TextBox(_app,width=33)
_qty_int = int(_qty_of_password.value) # THIS DOESN'T WORK
# I wants to run the code bellow to create a random password after I get password length and quantity of password.
#and print it to _welcome_message. this code works separetly.
for _p in range(_qty_of_password.value):
_password = ""
for _c in range(_length_int):
_password += random.choice(_chars)
print(_password)
# create a push button to transfer value of _my_name text Box to _welcome_massage
_button = PushButton(_app,command=_say_my_name,text="Click")
_app.display()
跟踪:
Traceback (most recent call last): File "C:/Users/Owner/PycharmProjects/Assignment_2/main.py",line 18,in <module> _length_int = int(_length.value) #THIS DOESN'T WORK ValueError: invalid literal for int() with base 10: ''
- https://projects.raspberrypi.org/en/projects/password-generator
- https://projects.raspberrypi.org/en/projects/getting-started-with-guis
解决方法
您太早设置了_length_int
,_qty_int
。
您应该将command
回调附加到文本框。
更改文本时要调用的函数的名称。该函数必须使用零或一个参数,如果该函数使用一个参数,则将返回添加到文本框中的键。
(https://lawsie.github.io/guizero/textbox/)
示例:
from guizero import App,Text,TextBox,PushButton,error
import random
# char for creating password
_chars = 'abcdefghijklmnopqrstwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.,!@#$%&'
def _say_my_name():
_welcome_message.value = "Your random value is: " + _length.value
_app = App(title="Password Creator")
message = Text(_app,text="Welcome")
_welcome_message = Text(_app,text="Create your random password here",size=19,font="Arial",color="hotpink")
#TextBox for input of password length
_length_label = Text(_app,text="Give me length of password:",size=11,font="Arial")
_length = TextBox(_app,width=33,command=text_box_changed)
_length_int = 0 #THIS DOESN'T WORK
#TextBox for input of password qty: 3 pcs/ 4pcs random password
_qty_label = Text(_app,text="How many password you want: ")
_qty_of_password = TextBox(_app,command=text_box_changed)
_qty_int = 0 #THIS DOESN'T WORK
# I wants to run the code bellow to create a random password after I get password length and quantity of password.
#and print it to _welcome_message. this code works separetly.
def text_box_changed():
if not _length.value or not _qty_of_password.value:
return
for _p in range(int(_qty_of_password.value)):
_password = ""
for _c in range(int(_length.value)):
_password += random.choice(_chars)
print(_password)
# create a push button to transfer value of _my_name text box to _welcome_massage
_button = PushButton(_app,command=_say_my_name,text="Click")
_app.display()
,
谢谢和抱歉。我做了些吼叫。现在我有两个问题。 01.密码未显示在表单上。但是在cmd提示符下它出现了。显示屏显示“您的随机值为:[Text]对象,文本为“ None””02。我不知道要使用哪个变量来构成用于显示多个密码的数组。感谢您的宝贵时间。
ARG ENV_VAR1_DEFAULT_VALUE=VAL1
ENV_VAR1=$ENV_VAR1_DEFAULT_VALUE
,
当 Python 无法将字符串转换为整数时会出现此错误。在这种情况下,您没有在文本框中输入任何内容,因此 Python 尝试将空字符串 (""
) 转换为 int
,但它失败了。
您必须在代码中添加以下几行:
try:
_length_int = int(_length.value)
except ValueError:
print('Invalid number!')