我想将字符串从 GUIzero 的 TextBox 转换为整数。
我的项目是创建一个 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: ''
我从以下链接获得的主要代码: