How to convert string to int from a TextBox of GUIzero?

904 views Asked by At

I wants to convert a string to an integer from TextBox of GUIzero.

My project is create a GUI form, from where I will grab length of a password and quantity of a password. Then I will show number of random passwords.

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:

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: ''

Main code I got from links the below:

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

There are 2 answers

0
norbeq On

You set the _length_int, _qty_int too early. You should attach the command callback to textbox.

The name of a function to call when the text is changed. This function MUST take either zero or one argument, if the function takes one argument the key which was added to the textbox will be returned.

(https://lawsie.github.io/guizero/textbox/)

Example:

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()
0
Shaik On

Thanks and Sorry. I did something as bellow. Now I got two problem. 01. The password didn't display on form. But in cmd prompt it appeared. The display shows " Your random value is: [Text] object with text "None" " 02. I don't under stand which variable to make an array for showing multiple password. Thanks and appreciate for your precious time.

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()