Lets say I have this sample code:
import wx
app = wx.App(False) # Create a new app, don't redirect stdout/stderr to a window.
frame = wx.Frame(None, wx.ID_ANY, "Hello World") # A Frame is a top-level window.
s=wx.Button(frame,-1,"New")
frame.Show(True) # Show the frame.
app.MainLoop()
Like in some programs, if you press the New button, it will open the program again in another window. I am wondering, how can i do this in python?
I cannot make a window object then make copies of it because, in my actual program I am using global variables and these global variables are made for only one window.
It appears that my only option is to find a way to run the program again.
You could try something like this
import subprocess
import sys
new_process = subprocess.Popen([sys.executable]+sys.argv)
Which will run python again with the arguments it was run with originally. Or modifying your original example to make pressing the button run the script again :-
import wx
import subprocess
import sys
def re_run(e):
new_process = subprocess.Popen([sys.executable]+sys.argv)
app = wx.App(False) # Create a new app, don't redirect stdout/stderr to a window.
frame = wx.Frame(None, wx.ID_ANY, "Hello World") # A Frame is a top-level window.
s=wx.Button(frame,-1,"New")
s.Bind(wx.EVT_BUTTON, re_run)
frame.Show(True) # Show the frame.
app.MainLoop()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With