user30558
user30558

Reputation: 43

How to use PySimpleGUI to build a GUI with a working python script

I'm using a program built in python 3 that figures out which VIP (Virtual IP) subnet I should use and which load balancer the VIP should match with. The program will ask you what the IP address is of one of the pool members. Based on that, it will tell you which subnet you will need an IP address for from the VIP and which load balancer to build the VIP on. An edited example of a simple working script is ran on a linux box shown below:

from netaddr import IPNetwork, IPAddress, IPSet

global subnet
global loadbalancer

member = IPAddress(input("What is the IP address of one of the nodes? "))

# Lists for each LB pair containing the node subnets.
loadbalancer1_node_IPs = IPSet([IPNetwork('1.1.1.0/22'), IPNetwork('2.2.2.0/22'), IPNetwork('3.3.3.0/22')])
loadbalancer2_node_IPs = IPSet([IPNetwork('4.4.4.0/23'), IPNetwork('2.2.2.0/23'), IPNetwork('3.3.3.0/23')])


# Provides the LB the VIP is to be built on based on node IP address.
if IPAddress(member) in loadbalancer1_node_IPs:
    loadbalancer = "The LB pair you need to build this VIP on is loadbalancer1_node_IPs"
    subnet = "You need a VIP ip from subnet 1.1.1.0/22, 2.2.2.0/22, or 3.3.3.0/22"
elif IPAddress(member) in loadbalancer2_node_IPs:
    loadbalancer = "The LB pair you need to build this VIP on is loadbalancer2_node_IPs"
    subnet = "You need a VIP ip from subnet 4.4.4.0/23, 2.2.2.0/23, or 3.3.3.0/23"


print(loadbalancer)
print(subnet)
exit(input("Press Enter to exit."))

This scripts work fine using the cli on linux. My goal now is to use the python package PySimpleGUI to build a GUI that outputs the results. I am trying to combine the code above with the below code for PySimpleGUI:

import PySimpleGUI as sg

form = sg.FlexForm('F5 Load Balancer VIP IP and LB selector')

layout = [ [sg.Text('What is the IP address of one of the nodes'), sg.InputText()],
           [sg.OK()] ]

button, (name,) = form.Layout(layout).Read()

I am having trouble getting a basic output of the results. I tried using

window = sg.Window('Test').Layout(layout) 

and updating it with

window.FindElement('loadbalancer').Update(loadbalancer) 

but the window displays the error

"Error creating layout. The layout specified has already been used.". 

Any assistance is appreciated. Thank you.

Upvotes: 4

Views: 1693

Answers (2)

Mike from PSG
Mike from PSG

Reputation: 5754

Working from the layouts furas posted, these are the coding conventions that are currently used with PySimpleGUI. You need to add calls to close your windows too.

Here's an updated version of the GUI based on the latest naming and coding conventions:

import vip
import PySimpleGUI as sg

layout = [ 
    [sg.Text('What is the IP address of one of the nodes'), sg.InputText(key='IP')],
    [sg.OK()] 
]

window = sg.Window('F5 Load Balancer VIP IP and LB selector', layout)

event, values = window.read()
window.close()
ip = values['IP']
loadbalancer, subnet = vip.run(ip)


layout = [ 
    [sg.Text(ip)],
    [sg.Text(loadbalancer)],
    [sg.Text(subnet)],
    [sg.OK()] 
]

window = sg.Window('F5 Load Balancer VIP IP and LB selector', layout)
event, values = window.read()
window.close()

Upvotes: 3

furas
furas

Reputation: 142651

It could be easier if you put code in function so you could import it to other script

vip.py

from netaddr import IPNetwork, IPAddress, IPSet

def run(ip):
    member = IPAddress(ip)

    # Lists for each LB pair containing the node subnets.
    loadbalancer1_node_IPs = IPSet([IPNetwork('1.1.1.0/22'), IPNetwork('2.2.2.0/22'), IPNetwork('3.3.3.0/22')])
    loadbalancer2_node_IPs = IPSet([IPNetwork('4.4.4.0/23'), IPNetwork('2.2.2.0/23'), IPNetwork('3.3.3.0/23')])

    # Provides the LB the VIP is to be built on based on node IP address.
    if IPAddress(member) in loadbalancer1_node_IPs:
        loadbalancer = "The LB pair you need to build this VIP on is loadbalancer1_node_IPs"
        subnet = "You need a VIP ip from subnet 1.1.1.0/22, 2.2.2.0/22, or 3.3.3.0/22"
    elif IPAddress(member) in loadbalancer2_node_IPs:
        loadbalancer = "The LB pair you need to build this VIP on is loadbalancer2_node_IPs"
        subnet = "You need a VIP ip from subnet 4.4.4.0/23, 2.2.2.0/23, or 3.3.3.0/23"
    else:
        loadbalancer = "?"
        subnet = "?"

    return loadbalancer, subnet

if __name__ == "__main__":
    # this part is not executed when script is imported to other script
    ip = input("What is the IP address of one of the nodes? ")
    loadbalancer, subnet = run(ip)
    print(loadbalancer)
    print(subnet)
    exit(input("Press Enter to exit."))

And now in GUI you can uses

import vip

loadbalancer, subnet = vip.run(ip)

like

gui.py

import vip
import PySimpleGUI as sg

form = sg.FlexForm('F5 Load Balancer VIP IP and LB selector')

layout = [ 
    [sg.Text('What is the IP address of one of the nodes'), sg.InputText(key='IP')],
    [sg.OK()] 
]

button, values = form.Layout(layout).Read()

ip = values['IP']
loadbalancer, subnet = vip.run(ip)

form = sg.FlexForm('F5 Load Balancer VIP IP and LB selector')

layout = [ 
    [sg.Text(ip)],
    [sg.Text(loadbalancer)],
    [sg.Text(subnet)],
    [sg.OK()] 
]

button, values = form.Layout(layout).Read()

Upvotes: 3

Related Questions