【发布时间】:2021-03-09 20:16:23
【问题描述】:
我正在构建一个自定义网络测试应用程序。我在他们受人尊敬的脚本中有不同方面的测试。但是,每次我运行脚本时,脚本的导入都会导致脚本运行。正在导入的脚本是 IPTestFinal。
如何在不运行脚本的情况下调用它?我在 Raspberry Pi 4 上运行 Python 3。
图形用户界面脚本:
import PySimpleGUI as sg
import IPTestFinal as IPTF # This is calling upon the other script. I have identified this is where it is causing the script to run.
layout = [[sg.Text("")], [sg.Button("WiFi Scan")], # Design the screen
[sg.Button('DHCP Test')],
[sg.Button('Static IP Test')],
[sg.Button('Speed Test')],
[sg.Button('Exit')]
]
# Create the window
window = sg.Window("Network Tester", layout, margins=(800, 400)) # Create the screen
# Create an event loop
while True: # Watches for each button to be pressed
event, values = window.read()
# End program if user closes window or
# Presses the OK button
if event == "Exit" or event == sg.WIN_CLOSED:
break
if event == 'DHCP Test':
IPTF.test_network()
window.close()
###!!!***!!!###IPTestFinal Script:
import netifaces as ni
def test_network():
interfaces = ni.interfaces()
for i in interfaces: # Loop to check each interface available to the system
if i != "lo": # Eliminates the loopback (lo) from being displayed in the results
try:
ni.ifaddresses(i) # Sets the current interface (i) being checked
gws = ni.gateways()
gateway = gws['default'][ni.AF_INET][0]
ip = ni.ifaddresses(i)[ni.AF_INET][0]['addr']
sm = ni.ifaddresses(i)[ni.AF_INET][0]['netmask']
print ("Network information for " + i + ":")
print ("IP: " + ip)
print ("Subnet Mask: " + sm)
print ("Gateway: " + gateway)
print ()
except: # If interface is not connected or no DHCP available
print (i + " is not connected or DHCP is not available. Try setting a static IP address.")
test_network()
只要我运行脚本,输出就会自动运行,我得到:
Network information for eth0:
IP: 192.168.1.101
Subnet Mask: 255.255.255.0
Gateway: 192.168.1.254
wlan0 is not connected or DHCP is not available. Try setting a static IP.
**主要问题:我的一些脚本需要用户输入,这会导致应用程序暂停,本质上是崩溃。我需要在按下按钮时运行脚本。不是在启动主脚本时。
**链接到如何或示例脚本以添加一个框以在 GUI 中显示脚本的结果会很棒!
【问题讨论】:
-
食谱页面上的文档中有很多示例 - pysimplegui.readthedocs.io/en/latest/cookbook 特别是 pysimplegui.readthedocs.io/en/latest/cookbook/… 主要问题。