0

我想将字符串从 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: ''

我从以下链接获得的主要代码:

  1. https://projects.raspberrypi.org/en/projects/password-generator
  2. https://projects.raspberrypi.org/en/projects/getting-started-with-guis
4

2 回答 2

0

你设置的_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, width=33, 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()
于 2020-09-07T22:36:26.383 回答
0

谢谢和抱歉。我做了如下的事情。现在我有两个问题。01. 密码没有显示在表格上。但是在cmd提示符下出现了。显示屏显示“您的随机值是:带有文本“无”的 [Text] 对象“02。我不明白要创建一个数组来显示多个密码的变量。感谢并感谢您宝贵的时间。

from guizero import App, Text, TextBox, PushButton, error
import random

def _say_my_password():
    _test_pass = Text(_app, text=text_box_changed())
    _welcome_message.value = "Your random value is: " + str(_test_pass)

# Define the form application
_app = App(title="Password Creator", width=700, height=300)
_chars = 'abcdefghijklmnopqrstwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.,!@#$%&'
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_init = 0

# TextBox for input of password qty
_qty_label = Text(_app, text="How many password you want: ")
_qty_of_password = TextBox(_app, width=33)
_qty_ini = 0

# Create random password
def text_box_changed():
    if not _length.value or not _qty_of_password.value:
        error("Input error", "You must type in numbers for height and width")

    # char for creating password
    _qty_ini = int(_qty_of_password.value)
    _length_init = int(_length.value)

    for _p in range(int(_qty_ini)):
        _password = ""
        for _c in range(int(_length_init)):
            _password += random.choice(_chars)
        print(_password)
    return


# create a push button to transfer value
_button = PushButton(_app, command=_say_my_password, text="Click")


_app.display()
于 2020-09-09T22:37:19.770 回答