如何使用 PySimpleGUI 通过有效的 python 脚本构建 GUI

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

我正在使用内置于 python 3 中的程序来确定我应该使用哪个 VIP(虚拟 IP)子网以及 VIP 应该与哪个负载平衡器匹配。该程序将询问您池成员之一的 IP 地址是什么。基于此,它会告诉您需要 VIP 的 IP 地址的子网以及要在哪个负载均衡器上构建 VIP。一个简单的工作脚本的编辑示例是 linux 框上的 运行,如下所示:

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."))

此脚本在 linux 上使用 cli 工作正常。我现在的目标是使用 python 包 PySimpleGUI 构建一个输出结果的 GUI。我正在尝试将上面的代码与下面的 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()

我无法获得基本的结果输出。我尝试使用

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

并更新为

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

但是 window 显示错误

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

如有任何帮助,我们将不胜感激。谢谢你。

如果您将代码放入函数中,这样您就可以将其导入到其他脚本中,这可能会更容易

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."))

现在您可以在 GUI 中使用

import vip

loadbalancer, subnet = vip.run(ip)

喜欢

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

根据 furas 发布的布局,这些是当前用于 PySimpleGUI 的编码约定。您也需要添加调用来关闭您的 windows。

这是基于最新命名和编码约定的更新版 GUI:

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