【问题标题】:How can I execute a child python scriptby clicking on a button on a parent script?如何通过单击父脚本上的按钮来执行子 python 脚本?
【发布时间】:2019-10-29 01:35:58
【问题描述】:

我目前正在为 GUI 训练 OOP。我正在使用 wxPython 库来创建我的窗口并对其进行自定义。

现在,我正在尝试通过单击其他脚本中的按钮来启动 python 脚本。

为此,我有 2 个程序,wx_Practicing.py 和 wx_Practicing_child.py,它们位于同一个文件夹中。

wx_Practicing.py

import wx
import time
import wx_Practicing_child
import threading
import os
import sys

class MainWindow(wx.Frame):
    def __init__(self):
         wx.Frame.__init__(self, None, wx.ID_ANY, "Test", 
         wx.DefaultPosition,(1000,850), wx.DEFAULT_FRAME_STYLE, wx.FrameNameStr)

    # Click counter and flag variable for the new frame opened
    self.click = 0
    self.OpenButtonFlag = 0

    # Sizer to definit the position of elements
    sizer_hori = wx.BoxSizer(wx.HORIZONTAL)
    sizer_verti = wx.BoxSizer(wx.VERTICAL)

    # Panel
    test_panel = PanelMainWindow(self)
    test_panel.SetSizer(sizer_verti)

    # Button to close the main frame and end the program
    btn_quit = wx.Button(test_panel, label ="Quit")
    btn_quit.Bind(wx.EVT_BUTTON, self.OnQuit)
    sizer_verti.Add(btn_quit)

    # Button which displays the number of click done on it since the
    # frame is opened
    btn_counter = wx.Button(test_panel, label="Click counter")
    sizer_verti.Add(btn_counter)
    btn_counter.Bind(wx.EVT_LEFT_DOWN, self.OnCount)

    # Button which open the child frame from wx_Practicing_child.py
    btn_new_frame = wx.Button(test_panel, label = "Open new frame")
    sizer_verti.Add(btn_new_frame)
    btn_new_frame.Bind(wx.EVT_LEFT_DOWN, self.OnNewFrame)

    self.Show()

# Method to quit the frame and close it
def OnQuit(self, event):
    self.Close()

#Method to count clicks
def OnCount(self, event):
    self.click +=1
    print(self.click)

# MEthod which open the child frame
def OnNewFrame(self, event):
    if self.OpenButtonFlag == 0 :
        print('aaaaaaaa')
        os.system('wx_Practicing_child.py')
        self.Show()
        print("New frame opened")
        self.OpenButtonFlag = 1
    else :
        print("Frame already launched, close it before opening a new one")

class PanelMainWindow(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

test = wx.App(False)
frame = MainWindow()

test.MainLoop()

wx_Practicing_child.py

import wx
class MainWindow_child(wx.Frame):
    def __init__(self):
          wx.Frame.__init__(self, None, wx.ID_ANY, "Test", 
          wx.DefaultPosition, (1000,850), wx.DEFAULT_FRAME_STYLE, wx.FrameNameStr)

    self.OpenButtonFlag = 0

    # Sizer
    sizer_hori = wx.BoxSizer(wx.HORIZONTAL)
    sizer_verti = wx.BoxSizer(wx.VERTICAL)

    # Panel
    test_panel_child = PanelMainWindow_child(self)
    test_panel_child.SetSizer(sizer_verti)

    # Button to quit the child frame
    btn_quit = wx.Button(test_panel_child, label ="Quit")
    btn_quit.Bind(wx.EVT_BUTTON, self.OnQuit)
    sizer_verti.Add(btn_quit)

    self.Show()

# Method used to close the child frame
def OnQuit(self, event):
    self.OpenButtonFlag = 0
    self.Close()

class PanelMainWindow_child(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

所以基本上,功能很简单。我启动 wx_Practicing.py 以打开父窗口,然后单击“打开新框架”按钮,出现 wx_Practicing_child.py 的子框架。如果我再次触发该按钮,则在我关闭前一个子窗口之前它什么也不做。

但是我尝试的时候,buttonFlag设置为1,所以进入循环,但是子框架没有出现。

所以我想知道如何使它工作。我现在别无选择。

谢谢。

【问题讨论】:

  • 你到底想要什么?你想启动多个子框架?
  • 我想启动 1 个且只有 1 个子框架,它将在未来包含一些东西。这就是为什么我放了一个标志来知道孩子是否存在。

标签: python-3.x user-interface button wxpython


【解决方案1】:

欢迎来到 StackOverflow。

问题在于您以错误的方式创建子框架。 你只需要换行:

os.system('wx_Practicing_child.py')

为:

child = wx_Practicing_child.MainWindow_child()

【讨论】:

  • 非常感谢,现在可以使用了。只是另一个问题,这两条线之间的主要区别是什么?我是 OOP python 编程的新手,所以我不知道所有的小差异。
  • @ClemZer 我认为问题如下:当您使用 os.system 时,您将在子 shell 中执行 wx_Practicing_child.py 并且因为您没有在 wx_Practicing_child.py 中创建 wx.App 的实例,所以什么也没有将显示。但是,我记得我在某处读到过,您只能拥有 wx.App 的实例,因此创建 wx.App 的实例也不起作用。
  • 是的,我在子脚本中使用 wx.App() 时遇到了问题。它在没有触发按钮的情况下打开了窗口。因此,如果我正确理解,要这样做,我必须创建我的类 MainWindow_child() 的实例才能显示子窗口?
  • @ClemZer 是的,这就是它的工作原理。您导入包含子类的模块,创建子类的实例并显示它。
  • 乐于助人:)
猜你喜欢
  • 1970-01-01
  • 2013-02-15
  • 2016-07-15
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多