如何使用PySimpleGUI构建具有工作python脚本的GUI
我正在使用一个内置于Python3中的程序,该程序计算出我应该使用哪个VIP(虚拟IP)子网,以及VIP应该与哪个负载平衡器匹配。程序将询问您池成员之一的IP地址。基于此,它将告诉您需要从VIP为哪个子网提供IP地址,以及在哪个负载平衡器上构建VIP。一个简单工作脚本的已编辑示例在linux设备上运行,如下所示:如何使用PySimpleGUI构建具有工作python脚本的GUI,python,python-3.x,f5,Python,Python 3.x,F5,我正在使用一个内置于Python3中的程序,该程序计算出我应该使用哪个VIP(虚拟IP)子网,以及VIP应该与哪个负载平衡器匹配。程序将询问您池成员之一的IP地址。基于此,它将告诉您需要从VIP为哪个子网提供IP地址,以及在哪个负载平衡器上构建VIP。一个简单工作脚本的已编辑示例在linux设备上运行,如下所示: from netaddr import IPNetwork, IPAddress, IPSet global subnet global loadbalancer member =
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)
但是窗口会显示错误
"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."))
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()
现在在GUI中,您可以使用
import vip
loadbalancer, subnet = vip.run(ip)
像
gui.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."))
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当前使用的编码约定。您还需要添加呼叫以关闭窗口 以下是基于最新命名和编码约定的GUI更新版本:
导入vip
将PySimpleGUI导入为sg
布局=[
[sg.Text('其中一个节点的IP地址是什么'),sg.InputText(key='IP'),
[sg.OK()]
]
window=sg.window('F5负载平衡器VIP IP和LB选择器',布局)
事件,值=window.read()
window.close()
ip=值['ip']
负载平衡器,子网=vip.run(ip)
布局=[
[sg.文本(ip)],
[sg.Text(负载平衡器)],
[sg.文本(子网)],
[sg.OK()]
]
window=sg.window('F5负载平衡器VIP IP和LB选择器',布局)
事件,值=window.read()
window.close()
所有创建外部函数的变量都是全局变量,因此使用全局子网
和全局负载平衡器
是无用的。Wordglobal
在函数内部用于通知函数它必须使用外部/全局变量,而不是创建局部变量。始终将问题中的完整错误消息(从Word“Traceback”开始)作为文本(而不是屏幕截图)提出来(不是注释)。还有其他有用的信息。.Update(loadbalancer)
中的负载平衡器是什么?此变量只存在于其他脚本中,您不需要导入,因此无法在GUI中访问。最好将代码放入第一个脚本的函数中,将此函数导入第二个脚本,然后在第二个脚本中执行。这样,您可以直接在变量中获得结果。您可以跳过退出(输入())
@furas谢谢您的回复。对不起,我应该澄清一下。我试图把代码“弗兰肯斯坦”在一起,但我哪儿也不去。我发布的脚本和PySimpleGUI代码分别工作得很好,这就是我发布它的方式,以显示它的基本功能。我只是不知道如何将脚本组合到GUI中。回顾一下代码,我发现我可以删除这个部分。