-2

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • There are many examples in the docs on the cookbooks page - https://pysimplegui.readthedocs.io/en/latest/cookbook/ especially https://pysimplegui.readthedocs.io/en/latest/cookbook/#asynchronous-window-with-periodic-update for the main issue. – nycynik Mar 10 '21 at 14:59

1 Answers1

1

Add if __name__ == '__main__': before test_network() in IPTestFinal.py to confirm if you run this script or not.

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": #eleminates 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.")

if __name__ == '__main__':
    test_network()

The main script revised to view the output in the GUI,

Enter image description here

import PySimpleGUI as sg
import IPTestFinal as IPTF

sg.theme('Dark')
sg.set_options(font=('Courier New', 11))

command_layout = [
    [sg.Button("WiFi Scan", size=(16, 1))],
    [sg.Button('DHCP Test', size=(16, 1))],
    [sg.Button('Static IP Test', size=(16, 1))],
    [sg.Button('Speed Test', size=(16, 1))],
]

column_layout = [
    [sg.Column(command_layout, vertical_alignment='top', pad=(0, 0), expand_y=True, key='-CMD-')],
    [sg.Button('Exit')]
]

layout = [[
    sg.Multiline(size=(80, 25), reroute_stdout=True, reroute_stderr=True),
    sg.Column(column_layout, vertical_alignment='top', expand_y=True),
]]

window = sg.Window("Network Tester", layout, finalize=True)

while True:

    event, values = window.read()
    if event == "Exit" or event == sg.WIN_CLOSED:
        break
    if event == 'DHCP Test':
        IPTF.test_network()

window.close()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jason Yang
  • 11,284
  • 2
  • 9
  • 23
  • That is amazing, you gave me code to tear apart and learn from. How did you ensure all of your code was highlighted? – NECROSUMBRA Mar 10 '21 at 19:12
  • Edit your post, then you can find how to mark code area. – Jason Yang Mar 10 '21 at 20:04
  • Not sure what happened, but you can check it here https://meta.stackoverflow.com/questions/271542/why-wont-the-system-allow-me-to-ask-questions-for-several-days – Jason Yang Mar 11 '21 at 03:09