I'm building a custom network testing application. I have different aspects of the test in their respected scripts. However, every time I run the script, the import of the script causes the script to run. The script being imported is IPTestFinal.
How do I call upon the script without it running? I am running Python 3 on a Raspberry Pi 4.
GUI Script:
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()
As soon as I run the script the output auto runs and I get:
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.
**The main issue: some of my scripts require user input which causes the application to pause, essentially crash. I need the scripts to run when called upon a button pushed. Not when the main script is initiated.
**Links to how or sample script to add a box to display results of the script in the GUI would be awesome!