【问题标题】:How to convert string to int from a TextBox of GUIzero?如何从 GUIzero 的 TextBox 将字符串转换为 int?
【发布时间】:2020-09-07 22:16:38
【问题描述】:

我想将 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

【问题讨论】:

  • 您遇到了什么错误,在哪一行?最好为我们提供完整的错误消息以及堆栈跟踪。 - 如果您在_qty_int = int(_qty_of_password.value) 行遇到错误,正如您所建议的那样,那么值_qty_of_password.value 不是包含整数值的字符串值。如果是这样,这将起作用。您应该打印出 _qty_of_password.valuetype(_qty_of_password.value) 以检查您是否假设这是一个简单的数字字符串
  • 感谢您的回复。错误是 - Traceback(最近一次调用最后一次):文件“C:/Users/Owner/PycharmProjects/Assignment_2/main.py”,第 18 行,在 _length_int = int(_length.value) #THIS DOESN'T WORK ValueError: int() 以 10 为基数的无效文字:''

标签: python guizero


【解决方案1】:

您将_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()

【讨论】:

    【解决方案2】:

    谢谢和抱歉。我做了如下的事情。现在我有两个问题。 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()
    

    【讨论】:

      猜你喜欢
      • 2015-11-12
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 2022-06-27
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      相关资源
      最近更新 更多