wxPython Custom Dialog loading incorrectly - python

I am trying to create a custom dialog to display information. It is activated on a button press, and that mechanism is working perfectly. However, the dialog itself is broken. Any help is appreciated.
import wx
class ForgotPass(wx.Dialog):
def __init__(self, *args, **kwargs):
super(ForgotPass, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
self.panel = wx.Panel(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.title = wx.StaticText(self.panel, label='Forgotten Your Password?', style=wx.ALIGN_CENTER)
self.title.SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, underline=True)) # Underlined & Bold
self.sizer.Add(self.title)
self.text = wx.StaticText(self.panel, label="Contact 'TheHiguty' via ingame PM to have your password reset!", style=wx.ALIGN_CENTER)
self.sizer.Add(self.text)
self.SetSizer(self.sizer)
self.SetSize((200, 150))
self.SetTitle('Forgotten Your Password')
self.Center()
self.Show(True)
def main():
app = wx.App(False)
ForgotPass(None)
app.MainLoop()
if __name__ == "__main__":
main()
However rather than displaying the text correctly, I get this:
Any help to fix this issue is greatly appreciated!

Add self.sizer.Fit(self.panel) after self.SetSizer(self.sizer) to fix the problem.
Although, if you do so, you'll find that the horizontal width is too small, so you might want to expand that. Complete code with fix:
import wx
class ForgotPass(wx.Dialog):
def __init__(self, *args, **kwargs):
super(ForgotPass, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
self.panel = wx.Panel(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.title = wx.StaticText(self.panel, label='Forgotten Your Password?', style=wx.ALIGN_CENTER)
self.title.SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, underline=True)) # Underlined & Bold
self.sizer.Add(self.title)
self.text = wx.StaticText(self.panel, label="Contact 'TheHiguty' via ingame PM to have your password reset!", style=wx.ALIGN_CENTER)
self.sizer.Add(self.text)
self.SetSizer(self.sizer)
self.sizer.Fit(self.panel)
self.SetSize((400, 150))
self.SetTitle('Forgotten Your Password')
self.Center()
self.Show(True)
def main():
app = wx.App(False)
ForgotPass(None)
app.MainLoop()
if __name__ == "__main__":
main()
Although, there is a way to create dialogs that look cleaner and looks more native to whatever OS the program is run on: use wx.MessageBox
Here's a simple example:
import wx
class MainWindow(wx.Frame):
def __init__(self, parent):
super(MainWindow, self).__init__(parent)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.button = wx.Button(self, wx.ID_ANY, label="Test")
self.sizer.Add(self.button)
self.Bind(wx.EVT_BUTTON, self.password_dialog, self.button)
self.SetSizer(self.sizer)
self.sizer.Fit(self)
self.SetTitle('Test')
self.SetSize((100, 100))
self.Centre()
self.Show(True)
def password_dialog(self, event):
wx.MessageBox(
'Contact `TheHiguty` blah blah blah',
'Forgotten your password?',
wx.OK|wx.ICON_ERROR)
if __name__ == '__main__':
app = wx.App()
MainWindow(None)
app.MainLoop()

I understand that this is an older question, but I would like to note what I use for custom dialog boxes and do not have the "noise / icon" (at least in my opinion). This is working with wxPython 2.9.5.0.
This is the code I use:
from wx.lib.pubsub import pub
def showConfirmDlg(message, caption, flag=wx.OK|wx.ICON_NONE):
msg = wx.MessageDialog(None, message=message,
caption=caption, style=flag)
msg.ShowModal()
msg.Destroy()
pub.subscribe(showConfirmDlg, 'dialog.confirm')
As you can see in the snippet I can use this function repeatedly for various messages and eliminate the need for multiple function definitions. To show the dialog box with the text in your example I would do this:
def onButtonPress(self, event):
pub.sendMessage('dialog.confirm',
message="Forgot your password?\n Contact 'TheHiguty' (rest of text)",
caption="Forgot Password?") # Could even just put caption=""
For further re-usability options you can replace the "flag=wx.OK|wx.ICON_NONE" with just "flag" and add "flag=wx.OK|(whatever style)" to your function call.
For more examples regarding Python and wxPython I highly recommend Mike Driscoll's blog site The Mouse vs The Python.I have found it amazingly informative and have learned a lot about wxPython from Mr Driscoll.
Hope this helps you and others!
--EDIT--
I should mention that I found wx.ICON_NONE by accident while trying to remove icons from my message boxes myself.

Related

How to switch panels in python using wxPython

I'm trying to create an App which allows the user to switch the information show using buttons. The basic idea of the code is that the user sees buttons on the left side of the screen and when the user presses "button 1", the code shows Panel1. I've made 2 buttons so far and the code for 2 panels is written as well but i can't figure out how to update my MainFrame so it show a different panel when one of the buttons is pressed.
Code:
import wx
TabNumber = 1
class ButtonPanel(wx.Panel):
def __init__(self, parent):
global TabNumber
super(ButtonPanel, self).__init__(parent, -1)
self.Tab1Button = wx.Button(self, label="TAB 1")
self.Tab1Button.Bind(wx.EVT_BUTTON, self.SwitchTab(1))
self.Tab2Button = wx.Button(self, label="TAB 2")
self.Tab2Button.Bind(wx.EVT_BUTTON, self.SwitchTab(2))
self.Sizer = wx.BoxSizer(wx.VERTICAL)
self.Sizer.Add(self.Tab1Button, wx.CENTER,0)
self.Sizer.Add(self.Tab2Button, wx.CENTER, 0)
self.SetSizer(self.Sizer)
def SwitchTab(self, tab):
def OnClick(event):
print(f"Switch to tab {tab} started")
TabNumber = tab
print(TabNumber)
return OnClick
class Panel1(wx.Panel):
def __init__(self, parent):
super(Panel1, self).__init__(parent, -1)
self.panel = wx.Panel(self)
self.text = wx.StaticText(self.panel, label="1")
class Panel2(wx.Panel):
def __init__(self, parent):
super(Panel2, self).__init__(parent, -1)
self.panel = wx.Panel(self)
self.text = wx.StaticText(self.panel, label="2")
class MainFrame(wx.Frame):
def __init__(self):
super(MainFrame, self).__init__(None, -1, "Test Application")
self.Panels = {
"Panel1": Panel1(self),
"Panel2": Panel2(self)
}
self.MySizer = wx.BoxSizer(wx.HORIZONTAL)
self.tabpanel = ButtonPanel(self)
self.MySizer.Add(self.tabpanel,wx.CENTER,0)
self.InfoPanel = self.Panels["Panel"+str(TabNumber)]
self.MySizer.Add(self.InfoPanel, wx.CENTER,0)
self.SetSizer(self.MySizer)
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()
I was also wondering how I can adjust the ratio for the space that is given to my ButtonPanel and my InfoPanel.
As far as I can see, you are trying to do something that works like a Wizard... On the one hand, you can use wx.adv.Wizard. On the other hand, you can look at this tutorial that does something very similar and adapt it to what you need:
WXPython: How to create a generic wizard
Good luck!

wxPython writing to disabled wx.TextCtrl

for a pet project of mine I am using a wxPython 2.8 and I have a dialog with a disabled wx.TextCtrl:
self.txt = wx.TextCtrl(self, wx.ID_ANY, size=(450,100),
style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
in the same dialog there is a button; when button is clicked I want to append some text (programmatically) to the TextCtrl. I tried by using
self.txt.AppendText('Hello')
but it doesn't work (Windows XP OS).
Is there any way to do that ?
thanks
You can use the wx.TextCtrl write function to achieve that.
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Test TextCtrl')
self.panel = wx.Panel(self)
self.text1 = wx.TextCtrl(self.panel,value="My Text",pos=(10,10),size=(350,30))
self.button = wx.Button(self.panel, -1, "Click",pos=(10,40))
self.button.Bind(wx.EVT_BUTTON, self.On_Button)
self.text1.Enable(False)
self.Show()
def On_Button(self, event):
self.text1.write(" Click ")
if __name__ == '__main__':
app = wx.App()
frame = MainFrame()
app.MainLoop()

wxPython Panel in existing window: slow and small

I'm experiencing very different behavior when creating a wx.Panel depending on whether the main window's already called Show().
With the below code, the application opens quickly with MainPanel created & populated before MainFrame.Show(). Using "New" to re-create it has multi-second lag while it re-makes the 200 texts. Also the MainPanel doesn't expand to take up the whole size of the window until the window is resized.
This is definitely an issue with panel creation rather than panel destruction; if I remove the MainPanel creation in MainFrame's init, then the New action has the same speed & size issues.
Two questions:
Is there anything I can do to speed up panel creation after MainFrame.Show()?
What needs to be done when creating MainPanel after MainFrame.Show()ed to have the MainPanel expand to the size of its parent?
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
import wx.lib.scrolledpanel
class MainPanel(wx.lib.scrolledpanel.ScrolledPanel):
def __init__(self,parent):
wx.lib.scrolledpanel.ScrolledPanel.__init__(self, parent=parent)
self.SetupScrolling()
sizer = wx.BoxSizer(wx.VERTICAL)
for i in range(1,200):
sizer.Add(wx.StaticText(self, wx.ID_ANY, "I'm static text"))
self.SetSizer(sizer)
self.SetAutoLayout(True)
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="FrameTest", size=(600,800))
self.InitMenu()
self.panel = None
self.panel = MainPanel(self)
def InitMenu(self):
self.menuBar = wx.MenuBar()
menuFile = wx.Menu()
menuFile.Append(wx.ID_NEW, "&New")
self.Bind(wx.EVT_MENU, self.OnNew, id=wx.ID_NEW)
self.menuBar.Append(menuFile, "&File")
self.SetMenuBar(self.menuBar)
def OnNew(self, evt):
if self.panel:
self.panel.Destroy()
self.panel = MainPanel(self)
if __name__ == "__main__":
app = wx.App(0)
frame = MainFrame()
frame.Show()
app.MainLoop()
UPDATE: joaquin's SendSizeEvent() definitely solves the first problem. For the second, I've found that hiding containers works out well. I'm guessing that after the window's been shown, it's trying to unnecessarily re-(display/layout/something) after every new widget and that's slowing it down. If I add Hide & Show to the panel's init, then there's no more lag and it works in both situations.
class MainPanel(wx.lib.scrolledpanel.ScrolledPanel):
def __init__(self,parent):
wx.lib.scrolledpanel.ScrolledPanel.__init__(self, parent=parent)
self.SetupScrolling()
self.Hide()
sizer = wx.BoxSizer(wx.VERTICAL)
for i in range(1,200):
sizer.Add(wx.StaticText(self, wx.ID_ANY, "I'm static text"))
self.SetSizer(sizer)
self.SetAutoLayout(True)
self.Show()
The panel will get the correct size if the Frame feels a SizeEvent. So this works for your second question:
def OnNew(self, evt):
if self.panel:
self.panel.Destroy()
self.panel = MainPanel(self)
self.SendSizeEvent()
Control of windows and widgets becomes easier by using sizers. With sizers in your main frame you can use the sizer Layout method to fit widgets in place.
Could not find a way of speeding up panel rewrite. But you can diminish the bizarre visual effect by not deleting the panel but clearing the sizer instead (you dont need SendSizeEvent() for this case):
class MainPanel(wx.lib.scrolledpanel.ScrolledPanel):
def __init__(self,parent):
wx.lib.scrolledpanel.ScrolledPanel.__init__(self, parent=parent)
self.SetupScrolling()
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.fill()
self.SetSizer(self.sizer)
def fill(self):
tup = [wx.StaticText(self, wx.ID_ANY, "I'm static text") for i in range(200)]
self.sizer.AddMany(tup)
self.Layout()
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="FrameTest", size=(600,800))
self.InitMenu()
self.panel = None
self.panel = MainPanel(self)
def InitMenu(self):
self.menuBar = wx.MenuBar()
menuFile = wx.Menu()
menuFile.Append(wx.ID_NEW, "&New")
self.Bind(wx.EVT_MENU, self.OnNew, id=wx.ID_NEW)
self.menuBar.Append(menuFile, "&File")
self.SetMenuBar(self.menuBar)
def OnNew(self, evt):
if self.panel:
self.panel.sizer.Clear()
self.panel.fill()

AuiNotebook, where did the event happend

How can I find out from which AuiNotebook page an event occurred?
EDIT: Sorry about that. Here are a code example. How do I find the notebook page
from witch the mouse was clicked in?
#!/usr/bin/python
#12_aui_notebook1.py
import wx
import wx.lib.inspection
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.nb = wx.aui.AuiNotebook(self)
self.new_panel('Pane 1')
self.new_panel('Pane 2')
self.new_panel('Pane 3')
def new_panel(self, nm):
pnl = wx.Panel(self)
self.nb.AddPage(pnl, nm)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.nb, 1, wx.EXPAND)
self.SetSizer(self.sizer)
pnl.Bind(wx.EVT_LEFT_DOWN, self.click)
def click(self, event):
print 'Mouse click'
#How can I find out from witch page this click came from?
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, '12_aui_notebook1.py')
frame.Show()
self.SetTopWindow(frame)
return 1
if __name__ == "__main__":
app = MyApp(0)
# wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
Oerjan Pettersen
For a mouse click you can assume the current selected page is the one that got the click. I added a few lines to your code. See comments
def new_panel(self, nm):
pnl = wx.Panel(self)
# just to debug, I added a string attribute to the panel
# don't you love dynamic languages? :)
pnl.identifierTag = nm
self.nb.AddPage(pnl, nm)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.nb, 1, wx.EXPAND)
self.SetSizer(self.sizer)
pnl.Bind(wx.EVT_LEFT_DOWN, self.click)
def click(self, event):
print 'Mouse click'
# get the current selected page
page = self.nb.GetPage(self.nb.GetSelection())
# notice that it is the panel that you created in new_panel
print page.identifierTag

wxpython auinotebook close tab event

What event is used when I close a tab in an auinotebook? I tested with
EVT_AUINOTEBOOK_PAGE_CLOSE(D). It didn't work.
I would also like to fire a right click on the tab itself event.
Where can I find all the events that can be used with the aui manager/notebook? Might just be my poor searching skills, but I can't find any lists over the different events that exist, not for mouse/window events either. It would be really handy to have a complete list.
#!/usr/bin/python
#12_aui_notebook1.py
import wx
import wx.lib.inspection
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.nb = wx.aui.AuiNotebook(self)
self.new_panel('Page 1')
self.new_panel('Page 2')
self.new_panel('Page 3')
self.nb.Bind(wx.EVT_AUINOTEBOOK_PAGE_CLOSED, self.close)
def new_panel(self, nm):
pnl = wx.Panel(self)
pnl.identifierTag = nm
self.nb.AddPage(pnl, nm)
self.sizer = wx.BoxSizer()
self.sizer.Add(self.nb, 1, wx.EXPAND)
self.SetSizer(self.sizer)
def close(self, event):
print 'closed'
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, '12_aui_notebook1.py')
frame.Show()
self.SetTopWindow(frame)
return 1
if __name__ == "__main__":
app = MyApp(0)
# wx.lib.inspection.InspectionTool().Show()
app.MainLoop()
Oerjan Pettersen
This is the bind command you want:
self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSED, self.close, self.nb)
To detect a right click on the tab (e.g. to show a custom context menu):
self.Bind(wx.aui.EVT_AUINOTEBOOK_TAB_RIGHT_DOWN, self.right, self.nb)
Here's a list of the aui notebook events:
EVT_AUINOTEBOOK_PAGE_CLOSE
EVT_AUINOTEBOOK_PAGE_CLOSED
EVT_AUINOTEBOOK_PAGE_CHANGED
EVT_AUINOTEBOOK_PAGE_CHANGING
EVT_AUINOTEBOOK_BUTTON
EVT_AUINOTEBOOK_BEGIN_DRAG
EVT_AUINOTEBOOK_END_DRAG
EVT_AUINOTEBOOK_DRAG_MOTION
EVT_AUINOTEBOOK_ALLOW_DND
EVT_AUINOTEBOOK_DRAG_DONE
EVT_AUINOTEBOOK_BG_DCLICK
EVT_AUINOTEBOOK_TAB_MIDDLE_DOWN
EVT_AUINOTEBOOK_TAB_MIDDLE_UP
EVT_AUINOTEBOOK_TAB_RIGHT_DOWN
EVT_AUINOTEBOOK_TAB_RIGHT_UP
From: {python folder}/Lib/site-packages/{wxpython folder}/wx/aui.py

Categories

Resources