segawe
segawe

Reputation: 35

Can you convert a Python script into GUI form?

I am completely new to python and I have been working with a script to generate a token(it also uses imports of other scripts), but now I want to use it on a GUI instead of over cmd always. Is this possible? I have tried using an example GUI and adding the elements to it, but I can't seem to figure out the output portion.

the main.py:

from encode_token import Encoder, Shared
from decode_token import Decoder
import codecs



dkey = (input("enter hex:\n"))
start_code = int(input("enter Starting code:\n"))

device_count = int(input("enter last count:\n"))
days_to_activate = int(input("enter days to activate:\n"))


if __name__ == '__main__':
    print('Generating code to: '+device_key_hex+' and starting code '+str(device_starting_code)+'. ')
  
    new_count, token = Encoder.generate_standard_token(
        starting_code=start_code,
        key=codecs.decode(dkey, 'hex'),
        value=days_to_activate,
        count=device_count,
        restricted_digit_set=False,
        mode=Shared.TOKEN_TYPE_ADD_TIME
    )

    print(token)

    value, count, type = Decoder.get_activation_value_count_and_type_from_token(
        starting_code=start_code,
        key=codecs.decode(dkey, 'hex'),
        token=token,
        last_count=device_count
    )
    print(value, count, type)

Upvotes: 1

Views: 3023

Answers (2)

Jason Yang
Jason Yang

Reputation: 13057

Try to build GUI by using PySimpleGUI for you, should confirm all imports OK. Refer https://pysimplegui.readthedocs.io/en/latest/ for documents about PySimpleGUI.

Install PySimpleGUI maybe by

pip install PySimpleGUI
#from encode_token import Encoder, Shared
#from decode_token import Decoder
#import codecs
#import Shared
import PySimpleGUI as sg

def update_result(token, value, count, type_):
    window['token'].update(str(token))
    window['value'].update(str(value))
    window['count'].update(str(count))
    window['type '].update(str(type_))

sg.theme('DarkBlue3')
sg.set_options(font=('Courier New', 16))

layout = [
    # Input
    [sg.Text('Hex Code        '), sg.Input(key='dkey'            )],
    [sg.Text('Starting Code   '), sg.Input(key='start_code'      )],
    [sg.Text('Last Count      '), sg.Input(key='device_count'    )],
    [sg.Text('Days to Activate'), sg.Input(key='days_to_activate')],

    # Event generated by a button
    [sg.Button('Generate')],

    # Horizontal Seperator
    [sg.HorizontalSeparator()],

    # Output
    [sg.Text('Token : '), sg.Text(size=(0, 1), key='token')],
    [sg.Text('Value : '), sg.Text(size=(0, 1), key='value')],
    [sg.Text('Count : '), sg.Text(size=(0, 1), key='count')],
    [sg.Text('Type  : '), sg.Text(size=(0, 1), key='type ')],
]

window = sg.Window("Token", layout)     # window layout and title

while True:

    event, values = window.read()       # Get event and values of elements
    if event == sg.WINDOW_CLOSED:       # If close button of window clicked
        break
    elif event == 'Generate':           # If Button 'Generate' clicked
        # Get all inputs
        try:
            start_code   = int(values['start_code'])        # if not integer
        except:
            update_result(*(("Wrong starting code !",)*4))  # Show warning information
            continue

        dkey             = values['dkey']
        device_count     = values['device_count']
        days_to_activate = values['days_to_activate']

        # Get result from inputs
        new_count, token = Encoder.generate_standard_token(
            starting_code=start_code,
            key=codecs.decode(dkey, 'hex'),
            value=days_to_activate,
            count=device_count,
            restricted_digit_set=False,
            mode=Shared.TOKEN_TYPE_ADD_TIME
        )

        value, count, type_ = Decoder.get_activation_value_count_and_type_from_token(
            starting_code=start_code,
            key=codecs.decode(dkey, 'hex'),
            token=token,
            last_count=device_count
        )

        # Show output
        update_result(token, value, count, type_)

window.close()

Upvotes: 1

Gingerbreadfork
Gingerbreadfork

Reputation: 98

As you're really new it might be worth checking out Gooey it's designed for making basic script to GUI apps that can turn out pretty nice, sounds like that might suit you here. For something more custom but still easier to get into, try PySimpleGUI it's pretty powerful but not difficult to get a handle on. There's of course tkinter as well but it's not that easy if you're super new to Python to use, while it is certainly easier than some other options.

Upvotes: 0

Related Questions