【问题标题】:Using a PySimpleGUI import script without running it使用 PySimpleGUI 导入脚本而不运行它
【发布时间】: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 中显示脚本的结果会很棒!

【问题讨论】:

标签: python-import pysimplegui


【解决方案1】:

IPTestFinal.py 中的test_network() 之前添加if __name__ == '__main__': 以确认您是否运行此脚本。

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

修改主脚本以在 GUI 中查看输出,

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

【讨论】:

  • 太棒了,你给了我代码让我撕开并从中学习。您如何确保所有代码都被突出显示?
  • 编辑你的帖子,然后你可以找到如何标记代码区域。
  • 不确定发生了什么,但你可以在这里查看meta.stackoverflow.com/questions/271542/…
猜你喜欢
  • 2016-09-17
  • 1970-01-01
  • 2015-04-11
  • 1970-01-01
  • 2010-09-13
  • 1970-01-01
  • 1970-01-01
  • 2019-03-18
  • 1970-01-01
相关资源
最近更新 更多