我目前正在为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时,它会进入循环,但不会显示子框架。
所以我想知道如何使它工作。我现在无法选择了。
谢谢。
答案 0 :(得分:1)
欢迎使用StackOverflow。
问题是您以错误的方式创建了子框架。 您只需要更改以下行:
os.system('wx_Practicing_child.py')
针对:
child = wx_Practicing_child.MainWindow_child()