Search code examples
python-3.xuser-interfacebuttonwxpython

How can I execute a child python scriptby clicking on a button on a parent script?


I am currently training OOP for GUI. I am using the wxPython library to create my windows and customize them.

Right now, I am trying to launch a python script by clicking on a button from an other script.

For that, I have 2 programs, wx_Practicing.py and wx_Practicing_child.py which are in the same folder.

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)

So basically, the functionning is simple. I launch wx_Practicing.py to open the parent window, then I click on the "Open new frame" button and the child frame from wx_Practicing_child.py appears. If i trigger the button again, it does nothing until I closed the previous child window.

But when I try it, the buttonFlag is set to 1, so it enters in the loop, but the child frame does not appear.

So I would like to know how could I make it work. I am out of option right now.

Thank you.


Solution

  • Welcome to StackOverflow.

    The problem is that you are creating the child frame in the wrong way. You only need to change the line:

    os.system('wx_Practicing_child.py')
    

    for:

    child = wx_Practicing_child.MainWindow_child()