wxPython: Why does modeless, non modal dialog stay on top of parent window? - dialog

I want to open a dialog modeless with Show(). After opening I can click on the parent window so the dialog opened successfully modeless.
The problem I have is that the modeless dialog always stays on top of the parent window. I want the parent to get on top when I click on it.
It doesn't matter wether I give the dialog a parent window or not. I test for the wx.STAY_ON_TOP style which is False for the dialog.
With simple code below (copied and modified from https://www.tutorialspoint.com/wxpython/wx_dialog_class.htm) I'm having the same problem.
How can I get a modeless dialog that can get behind the parent window?
import wx
# -----------------------------------------------------------------------------------
class MyDialog(wx.Dialog):
def __init__(self, parent, title):
super(MyDialog, self).__init__(parent, title=title, size=(250, 150))
panel = wx.Panel(self)
self.btn = wx.Button(panel, wx.ID_OK, label="ok", size=(50, 20), pos=(75, 50))
style = self.GetWindowStyle()
if style & wx.STAY_ON_TOP:
print('STAY_ON_TOP = True')
else:
print('STAY_ON_TOP = False')
# -----------------------------------------------------------------------------------
class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title=title, size=(300, 300))
self.InitUI()
def InitUI(self):
panel = wx.Panel(self)
btn1 = wx.Button(panel, label="Modal Dialog", pos=(20, 10))
btn2 = wx.Button(panel, label="Modeless Dialog Parent", pos=(20, 40))
btn3 = wx.Button(panel, label="Modeless Dialog Parentless", pos=(20, 70))
btn1.Bind(wx.EVT_BUTTON, self.OnModal)
btn2.Bind(wx.EVT_BUTTON, self.OnModelessParent)
btn3.Bind(wx.EVT_BUTTON, self.OnModelessParentless)
self.Show(True)
def OnModal(self, event):
MyDialog(self, "Dialog").ShowModal()
def OnModelessParent(self, event):
dlg = MyDialog(self, "Dialog").Show()
def OnModelessParentless(self, event):
dlg = MyDialog(None, "Dialog").Show()
# -----------------------------------------------------------------------------------
ex = wx.App()
Mywin(None, 'Modal / Modeless')
ex.MainLoop()

Adding the style wx.DIALOG_NO_PARENT to your dialog's constructor allows the dialogs to float behind the frame. Ex:
import wx
# -----------------------------------------------------------------------------------
class MyDialog(wx.Dialog):
def __init__(self, parent, title):
super(MyDialog, self).__init__(parent, title=title, size=(250, 150),
style=wx.DEFAULT_DIALOG_STYLE | wx.DIALOG_NO_PARENT)
panel = wx.Panel(self)
self.btn = wx.Button(panel, wx.ID_OK, label="ok", size=(50, 20), pos=(75, 50))
style = self.GetWindowStyle()
if style & wx.STAY_ON_TOP:
print('STAY_ON_TOP = True')
else:
print('STAY_ON_TOP = False')
# -----------------------------------------------------------------------------------
class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title=title, size=(300, 300))
self.InitUI()
def InitUI(self):
panel = wx.Panel(self)
btn1 = wx.Button(panel, label="Modal Dialog", pos=(20, 10))
btn2 = wx.Button(panel, label="Modeless Dialog Parent", pos=(20, 40))
btn3 = wx.Button(panel, label="Modeless Dialog Parentless", pos=(20, 70))
btn1.Bind(wx.EVT_BUTTON, self.OnModal)
btn2.Bind(wx.EVT_BUTTON, self.OnModelessParent)
btn3.Bind(wx.EVT_BUTTON, self.OnModelessParentless)
self.Show(True)
def OnModal(self, event):
MyDialog(self, "Dialog").ShowModal()
def OnModelessParent(self, event):
MyDialog(self, "Dialog").Show()
def OnModelessParentless(self, event):
MyDialog(None, "Dialog").Show()
# -----------------------------------------------------------------------------------
ex = wx.App()
Mywin(None, 'Modal / Modeless')
ex.MainLoop()
I removed the variable assignment dlg = on your 2 modeless event handlers because Show() returns True not the dialog instance.
It should also be noted that dialogs aren't automatically destroyed and you must call Destroy() manually to release their memory when you're done with them.

Related

How can i load class in notebook when tab is clicked in wxpython?

Here i have a doubt that when notebook tab is clicked at that time only it should load class in that tab. But in wxpython all class loads by default in tab so how can i put conditions to load class when tab is clicked.
Here is a small example.
import wx
class tabclass(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "This is the help tab", (20,20))
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="notebook")
mainPanel = wx.Panel(self,size=(1365, 700), pos=(0, 71), style=wx.DOUBLE_BORDER)
self.nb = wx.Notebook(mainPanel,size=(1365, 700))
tab0 = tabclass(self.nb)
self.nb.AddPage(tab0, "Tab One")
if __name__ == "__main__":
app = wx.App()
MainFrame().Show()
app.MainLoop()
Here I want to display static text when the tab is clicked otherwise class should not load.
You might want a different method of selecting what is and what is not displayed in the notebook.
A menu, for example, seems an appropriate selection tool.
i.e.
import wx
class tabclass(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
t = wx.StaticText(self, -1, "This is the help tab", (20,20))
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="notebook")
mainPanel = wx.Panel(self,size=(1365, 700), pos=(0, 71), style=wx.DOUBLE_BORDER)
self.nb = wx.Notebook(mainPanel,size=(1365, 700))
tabmenu = wx.Menu()
t1 = wx.MenuItem(tabmenu, id = -1, text="Help", kind=wx.ITEM_CHECK)
tabmenu.Append(t1)
t2 = wx.MenuItem(tabmenu, id = -1, text="Other tab", kind=wx.ITEM_CHECK)
tabmenu.Append(t2)
t3 = wx.MenuItem(tabmenu, id = -1, text="Another tab", kind=wx.ITEM_CHECK)
tabmenu.Append(t3)
tabmenu.Append(wx.ID_EXIT, '&Quit')
# Creating the menubar.
menuBar = wx.MenuBar()
# Add menus
menuBar.Append(tabmenu, "&Tabs")
# Adding the MenuBar to the Frame content.
self.SetMenuBar(menuBar)
# Bind menu item to functions
self.Bind(wx.EVT_MENU, self.helptab, t1)
self.Bind(wx.EVT_MENU, self.othertab, t2)
self.Bind(wx.EVT_MENU, self.anothertab, t3)
self.Bind(wx.EVT_MENU, self.OnQuit, id=wx.ID_EXIT)
# Obviously, here you would differentiate your tabs
# I have used the same one for brevity
def helptab(self, event):
if event.IsChecked():
self.tab0 = tabclass(self.nb)
self.nb.AddPage(self.tab0, "Help Tab")
else:
#Delete the "Help Tab"
pass
def othertab(self, event):
if event.IsChecked():
self.tab1 = tabclass(self.nb)
self.nb.AddPage(self.tab1, "Other Tab")
else:
#Delete the "Other Tab"
pass
def anothertab(self, event):
if event.IsChecked():
self.tab2 = tabclass(self.nb)
self.nb.AddPage(self.tab2, "Another Tab")
else:
#Delete the "Another Tab"
pass
def OnQuit(self, event):
self.Destroy()
if __name__ == "__main__":
app = wx.App()
MainFrame().Show()
app.MainLoop()

How to Switch Between Panels whitch Buttons

I can not navigate between the panels of my App through the buttons. I followed a lot of tutorials like this one but none solve my problem.
Thank for Help.
####################-----Principal Panel (first Panel)
import wx
from OtherPanel import OtherPanel
class MyPanel(wx.Panel):
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent = parent, size=(600, 500))
self.SetBackgroundColour(wx.Colour(250, 250, 250))
parent.Center()
self.histoPanel = OtherPanel(self)
self.home()
self.panel = wx.Panel(self)
def home(self):
self.panel = wx.Panel(self)
self.panel.SetBackgroundColour(wx.Colour(250, 250, 250))
bS =wx.Button(self, -1,"Go some where ",(200,30), size=(150,150))
self.Bind(wx.EVT_BUTTON, self.Sy, bS)
bOthe = wx.Button(self, -1,"Go to OtherPanel",(400,30),(150,150))
self.Bind(wx.EVT_BUTTON, self.onOpenFrame,bOthe)
def onOpenFrame(self, event):
"""
Opens secondary frame
"""
self.Hide()
self.histoPanel.Show()
def Sy(self, event):
"""
I have another panel (panel 3)
"""
self.Hide()
#self.panel3.Show()
--Second Panel (another Panel)
this panel is displayed when you click on the button of the first panel
import wx
class OtherPanel(wx.Panel):
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent = parent)
self.panel = wx.Panel(self)
self.SetBackgroundColour(wx.Colour(250, 250, 250))
self.Center()
self.homeHist()
self.Show()
def homeHist(self):
bBack = wx.Button(self, -1, "", pos=(20,30), size=(50,50))
self.Bind(wx.EVT_BUTTON, self.onClose, bBack)
def onClose(self, event):
self.Close()
------My Frame---.
here is my Frame/Windows. he must wear all the panels in turn
import wx
from MyPanel import MyPanel
from OtherPanel import OtherPanel
class MyFrame(wx.Frame):
""""""
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, size = (600,500))
self.myPanel = MyPanel(self)
self.otherpanel = OtherPanel(self)
self.otherpanel.Hide()
self.menu()
self.CreateStatusBar(style= wx.BORDER_NONE)
self.SetBackgroundColour("gray")
self.SetStatusText("\tthank")
def menu(self):
menubar = wx.MenuBar()
fileMenu = wx.Menu()
menubar.SetBackgroundColour(wx.Colour(1, 1, 6))
fileMenu.AppendSeparator()
live = wx.MenuItem(fileMenu, wx.ID_EXIT, '&Quit\tCtrl+Q', '\tlive my app')
fileMenu.Append(live)
self.Bind(wx.EVT_MENU, self.Onexit, live)
menubar.Append(fileMenu, '\t&Menu')
self.SetMenuBar(menubar)
def Onexit(self, event):
self.Close(True)
def Bouton(self, event):
if self.myPanel.IsShown():
self.SetTitle("Panel Two Showing")
self.myPanel.Hide()
self.otherpanel.Show()
else:
self.SetTitle("Panel One Showing")
self.myPanel.Show()
self.otherpanel.Hide()
self.Layout()
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
frame.Show()
app.MainLoop()
In your MyPanel.__init__ you are creating an instance of OtherPanel assigned to self.histoPanel as a child of the MyPanel, and then MyPanel's event handlers are hiding itself, which in turn hides its children including the self.histoPanel. You're also creating some panels assigned to self.panel which don't seem to be used for anything.
The way you are doing it in the MyFrame class is much better. It creates both panels and hides one. Then in MyFrame.Bouton it hides one and shows the other, depending on the current state. So what you need to do is get rid of all the extra panels in MyPanel (self.histoPanel and also the various self.panels that are never used for anything) and then you just need to have the button event ask the parent frame to swap the panels instead of trying to do it itself.
Here is an example of what Robin is suggesting in his answer:
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.panel = wx.Panel(self)
self.btn = wx.Button(self.panel, label="Panel 1", size=(250,75))
self.btn.Bind(wx.EVT_BUTTON, self.switch)
vbox1 = wx.BoxSizer(wx.VERTICAL)
vbox1.Add(self.btn)
self.panel.SetSizer(vbox1)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(self.panel)
self.SetSizer(vbox)
self.Show()
def switch(self, event):
self.parent.Swap()
class MyOtherPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.panel = wx.Panel(self)
self.btn = wx.Button(self.panel, label="Panel 2", size=(175,250))
self.btn.Bind(wx.EVT_BUTTON, self.switch)
vbox1 = wx.BoxSizer(wx.VERTICAL)
vbox1.Add(self.btn)
self.panel.SetSizer(vbox1)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(self.panel)
self.SetSizer(vbox)
self.Show()
self.panel.Hide()
def switch(self, event):
self.parent.Swap()
class PanelSwitcher(wx.Frame):
def __init__(self):
super().__init__(None)
vbox = wx.BoxSizer(wx.VERTICAL)
self.panel1 = MyPanel(self)
self.panel2 = MyOtherPanel(self)
vbox.Add(self.panel1)
vbox.Add(self.panel2)
self.SetSizer(vbox)
self.Show()
def Swap(self):
if self.panel1.panel.IsShown():
self.panel1.panel.Hide()
self.panel2.panel.Show()
else:
self.panel2.panel.Hide()
self.panel1.panel.Show()
self.Layout()
if __name__ == "__main__":
app = wx.App()
PanelSwitcher()
app.MainLoop()

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

wxPython, making a window modal

The following code makes a window to behave like a wx.Dialog but it works perfectly only in Windows. In macOS the buttons in the main window are not disabled and in Linux the main window can be maximized and minimized.
How can I have in macOS and Linux the same modal behavior I have in Windows? I know I could disable the buttons manually and I guess there should be a way to handle the minimize and maximize buttons, but perhaps there is an easier way.
import wx
class MyFrame(wx.Frame):
def __init__(self):
title='Multiple consecutive windows (ShowModal)'
super().__init__(None, title=title)
self.WinNum = 5
#### Widgets
self.panel = wx.Panel(self)
self.buttonShow = wx.Button(self.panel, pos=(50, 50), label='ShowModal')
self.buttonTest = wx.Button(self.panel, pos=(50, 100), label='Test')
#### Bind
self.buttonShow.Bind(wx.EVT_BUTTON, self.ShowWindows)
#### Position of the window
self.SetPosition(pt=(50, 50))
def ShowWindows(self, event):
i = 0
while i < self.WinNum:
a = WinModal(i)
a.ShowModal()
i += 1
class WinModal(wx.Frame):
def __init__(self, ThisWinNum):
title = 'This is window number: ' + str(ThisWinNum)
super().__init__(None, title=title)
#### Variables
self.ThisWinNum = ThisWinNum
#### Widgets
self.panel = wx.Panel(self)
self.buttonOk = wx.Button(self.panel, pos=(50, 50), label='Ok')
#### Positions
self.SetPosition(pt=(200, 200))
#### Bind
self.Bind(wx.EVT_CLOSE, self.OnClose)
self.Bind(wx.EVT_BUTTON, self.onOk)
def ShowModal(self):
"""
This function is the one giving the wx.FileDialog behavior
"""
self._disabler = wx.WindowDisabler(self)
self.Show()
self.eventLoop = wx.GUIEventLoop()
self.eventLoop.Run()
def OnClose(self, event):
"""
To handle closing the windows because you need to exit the eventLoop
of the modal window.
"""
del self._disabler
self.eventLoop.Exit()
self.Destroy()
def onOk(self, event):
print(self.ThisWinNum)
self.cancel = False
self.OnClose(event)
if __name__ == '__main__':
app = wx.App()
frame = MyFrame()
frame.Show()
app.MainLoop()
else:
pass
Probably my question was not correctly formulated. Now I realize that what I wanted to know was how to create a custom dialog window.
This can be done by deriving the custom class from wx.Dialog instead of wx.Frame. The class wx.Dialog gives an empty space that you can fill with widgets just like a normal wx.Frame and also has the ShowModal() method in it. Therefore, you can show your window as a modal window.
import wx
class MainWin(wx.Frame):
def __init__(self):
super().__init__(None, title='My modal window')
####---- Variables
self.frameFvar = None
####---- Widgets
self.panel = wx.Panel(self)
self.buttonF = wx.Button(self.panel, label='Show Normal')
self.buttonM = wx.Button(self.panel, label='Show Modal')
####---- Sizer
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.sizer.Add(self.buttonF, border=20, flag=wx.ALIGN_CENTER|wx.ALL)
self.sizer.Add(self.buttonM, border=20, flag=wx.ALIGN_CENTER|wx.ALL)
self.sizerM = wx.BoxSizer(wx.HORIZONTAL)
self.sizerM.Add(self.sizer, border=20,
flag=wx.EXPAND|wx.ALIGN_CENTER|wx.ALL)
self.panel.SetSizer(self.sizerM)
self.sizerM.Fit(self)
####---- Position
self.SetPosition(pt=(50, 50))
####---- Bind
self.buttonF.Bind(wx.EVT_BUTTON, self.ShowAsNormal)
self.buttonM.Bind(wx.EVT_BUTTON, self.ShowAsModal)
#---
def ShowAsNormal(self, event):
if self.frameFvar == None:
self.frameF = AsFrame(self)
self.frameF.Show()
self.frameFvar = True
else:
self.frameF.Raise()
#---
def ShowAsModal(self, event):
self.frameM = AsDialog(self)
if self.frameM.ShowModal() == wx.ID_OK:
print("Exited by Ok button")
else:
print("Exited by X button")
self.frameM.Destroy()
#---
#---
class AsFrame(wx.Frame):
def __init__(self, parent):
super().__init__(parent=parent, title='Shown as a wx.Frame')
####---- Variables
self.parent = parent
####---- Widgets
self.a = MyPanel(self)
####---- Position
self.SetPosition(pt=(50, 200))
####---- Bind
self.Bind(wx.EVT_CLOSE, self.OnClose)
#---
def OnClose(self, event):
self.parent.frameFvar = None
self.Destroy()
#---
#---
class AsDialog(wx.Dialog):
def __init__(self, parent):
super().__init__(parent=parent, title='Shown as a wx.Dialog')
####---- Variables
self.SetEscapeId(12345)
####---- Widgets
self.a = MyPanel(self)
self.buttonOk = wx.Button(self, wx.ID_OK)
####---- Sizers
self.sizerB = wx.StdDialogButtonSizer()
self.sizerB.AddButton(self.buttonOk)
self.sizerB.Realize()
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.a, border=10, flag=wx.EXPAND|wx.ALIGN_LEFT|wx.ALL)
self.sizer.Add(self.sizerB, border=10,
flag=wx.EXPAND|wx.ALIGN_RIGHT|wx.ALL)
self.SetSizer(self.sizer)
####---- Position
self.SetPosition(pt=(550, 200))
#---
#---
class MyPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent=parent)
####---- Variables
self.parent = parent
####---- Widgets
label = ("The same window shown as a wx.Frame or a wx.Dialog")
self.text = wx.StaticText(self, label=label, pos=(10, 10))
#---
#---
if __name__ == '__main__':
app = wx.App()
frameM = MainWin()
frameM.Show()
app.MainLoop()

how entrer value in new window and passing values of wx.newindow to wx.Frame wxpython

i have a panel with button Dynamic and when i click in button i have a new window opened the problem that i need zone to enter values to edit parameter of dynamic image like that :
that my code :
import wx
class MainFrame(wx.Frame):
def __init__(self,parent):
wx.Frame.__init__(self,parent,title="Myfirst",size=(800,580))
self.top = wx.Panel(self, style = wx.SUNKEN_BORDER)
self.bottom = wx.Panel(self ,style = wx.SUNKEN_BORDER)
self.left = wx.Panel(self ,style = wx.SUNKEN_BORDER, size = (250,-1))
st1 = wx.StaticText(self.bottom, -1, "show info ")
self.bottom.SetBackgroundColour('white')
dynamic=wx.Button(self.left,-1,"Dynamique",size=(110,30),pos=(50,100))
self.Bind(wx.EVT_BUTTON, self.newwindow, dynamic)
sizer1 = wx.BoxSizer(wx.VERTICAL)
sizer1.Add(self.top,1,wx.EXPAND,5)
sizer1.Add(self.bottom,1,wx.EXPAND,5)
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
sizer2.Add(self.left,0,wx.EXPAND,5)
sizer2.Add(sizer1,1,wx.EXPAND,5)
self.SetSizer(sizer2)
def newwindow(self, event):
secondWindow = window2(parent=self.left)
secondWindow.Show()
class window2(wx.Frame):
title = "new Window"
def __init__(self,parent):
wx.Frame.__init__(self,parent, -1,'Dynamic of image', size=(300,100))
panel=wx.Panel(self, -1)
self.SetBackgroundColour(wx.Colour(100,100,100))
self.Centre()
self.Show()
app = wx.App()
frame=MainFrame(None).Show()
app.MainLoop()
how can i add the zone to edit parametre like picture ?
i not sure if the newwindow what i need or dialog !!
thanks for help
I guess you will be fine with a normal new window. You can get the zone to write the parameters with a wx.TextCtrl widgets. You will need a way to export the values typed into the wx.TextCtrl so I added the style wx.TE_PROCESS_ENTER. With this style when you finish typing and press Enter you can process the typed values.
Also, there is no need to use Show() two times (secondWindow.Show() and self.Show()). One of them is enough.
Code with comments:
import wx
class MainFrame(wx.Frame):
def __init__(self,parent):
wx.Frame.__init__(self,parent,title="Myfirst",size=(800,580))
self.top = wx.Panel(self, style = wx.SUNKEN_BORDER)
self.bottom = wx.Panel(self ,style = wx.SUNKEN_BORDER)
self.left = wx.Panel(self ,style = wx.SUNKEN_BORDER, size = (250,-1))
st1 = wx.StaticText(self.bottom, -1, "show info ")
self.bottom.SetBackgroundColour('white')
dynamic=wx.Button(self.left,-1,"Dynamique",size=(110,30),pos=(50,100))
self.Bind(wx.EVT_BUTTON, self.newwindow, dynamic)
sizer1 = wx.BoxSizer(wx.VERTICAL)
sizer1.Add(self.top,1,wx.EXPAND,5)
sizer1.Add(self.bottom,1,wx.EXPAND,5)
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
sizer2.Add(self.left,0,wx.EXPAND,5)
sizer2.Add(sizer1,1,wx.EXPAND,5)
self.SetSizer(sizer2)
def newwindow(self, event):
secondWindow = window2(parent=self.left)
secondWindow.Show()
class window2(wx.Frame):
title = "new Window"
def __init__(self,parent):
"""
This is similar to the class MainFrame. You define a parent wx.Panel
and all other widgets are his childs.
"""
wx.Frame.__init__(self,parent, -1,'Dynamic of image', size=(300,100))
self.panel=wx.Panel(self, -1, style=wx.SUNKEN_BORDER)
self.st = wx.StaticText(self.panel, label='modifier bornes de la dynamique', style=wx.ALIGN_CENTER)
#### Notice the wx.TE_PROCESS_ENTER style to trigger processing the input when
#### Enter is pressed. Another alternative is to put a button somewhere.
self.text = wx.TextCtrl(self.panel, size=(200, 20), style=wx.SUNKEN_BORDER|wx.TE_PROCESS_ENTER)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.st, 0, wx.EXPAND|wx.ALL, 5)
self.sizer.Add(self.text, 0, wx.ALIGN_CENTER|wx.ALL, 5)
self.panel.SetSizer(self.sizer)
self.sizer.Fit(self.panel)
#self.SetBackgroundColour(wx.Colour(100,100,100))
self.Centre()
#### No need to use Show() here since you already use it in MainFrame.newwindow()
self.Show()
#### To execute self.onEnter when Enter is pressed inside self.text
self.Bind(wx.EVT_TEXT_ENTER, self.onEnter)
def onEnter(self, event):
#### Change it to fit your needs
print(self.text.GetValue())
self.Destroy()
app = wx.App()
frame=MainFrame(None).Show()
app.MainLoop()

Resources