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()
Related
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!
i have two py files that each have its own frame made using wxPython, MainWindow and RecWindow. MainWindow has the RecWindow python file included using the keyword "recovery".
MainWindow code:
class MainWindow(wx.Frame):
def __init__(self,parent,id,title):
wx.Frame.__init__(self, parent, wx.ID_ANY,title,pos=(500,200), size = (650,500), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
self.Bind(wx.EVT_CLOSE,self.OnExit)
self.SetIcon(wx.Icon('etc\icons\download.ico', wx.BITMAP_TYPE_ICO))
panel = wx.Panel(self)
RecWindow code:
class RecWindow(wx.Frame):
def __init__(self,parent,id,title):
wx.Frame.__init__(self, parent, wx.ID_ANY,title,pos=(400,200), size = (700,600), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
self.SetIcon(wx.Icon('etc\icons\download.ico', wx.BITMAP_TYPE_ICO))
self.count = 0
when i click on a button in MainWindow , it will hide the MainWindow create an instance of RecWindow, shown below;
def OpenRec(self,event):#this will be used to open the next frame
OR = recovery(None,-1,"RAVE")
OR.Show(True)
MainWindow.Hide()
now, what i am unsure of is how i can return to the MainWindow once i close the RecWindow. RecWindow has a cancel and finish button which both map to a self.close() function. How would i then get MainWindow to show itself again?
Use pubsub to send a message to the main window telling it to Show itself again. I actually have an example of how to do that here:
http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/
Note that this tutorial is using slightly older API that was available in wxPython 2.8. If you're using wxPython 2.9, then you'll have to use the slightly different API that I detail in this article:
http://www.blog.pythonlibrary.org/2013/09/05/wxpython-2-9-and-the-newer-pubsub-api-a-simple-tutorial/
When you create an instance of RecWindow keep a reference to it on main_window and bind to its close event.
In the main_window's close handler check if the window closed was the RecWindow.
If it was, clear the reference to it and show the main_window.
Elif the closed window was main_window carry out any required code.
Finally call event.Skip() so the windows get destroyed.
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title, (500, 200), (650, 500),
wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
panel = wx.Panel(self)
button = wx.Button(panel, wx.ID_OPEN)
panel.sizer = wx.BoxSizer(wx.VERTICAL)
panel.sizer.Add(button, 0, wx.ALL, 7)
panel.SetSizer(panel.sizer)
button.Bind(wx.EVT_BUTTON, self.on_button)
self.Bind(wx.EVT_CLOSE, self.on_close)
self.rec_window = None
def on_button(self, event):
rec_window = RecWindow(self, 'Rec window')
rec_window.Show()
self.Hide()
rec_window.Bind(wx.EVT_CLOSE, self.on_close)
self.rec_window = rec_window
def on_close(self, event):
closed_window = event.EventObject
if closed_window == self.rec_window:
self.rec_window = None
self.Show()
elif closed_window == self:
print 'Carry out your code for when Main window closes'
event.Skip()
class RecWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title, (400, 200), (700, 600),
wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
app = wx.App(False)
main_window = MainWindow(None, 'Main window')
main_window.Show()
app.MainLoop()
I have wxpython app that open wx.DirDialog on button click.
dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE)
if dlg.ShowModal() == wx.ID_OK:
# Do some stuff
Since my application is multithreaded and uses wxTaskbaricon which allow user (on Win 7) to close application even when modal DirDialog is open, I want to close the DirDialog before closing main app. Somehow non of below method work:
dlg.Destroy()
dlg.Close(True)
It is my testing code.
I can test Destroy(), Close() and EndModal() on Modal and Non-Modal wx.DirDialog()
To close modal dialog I had to use Timer - because modal dialog is blocking access to the main window.
It can't close dialog only if I do
self.dlg = None
self.dlg.EndModal(wx.CANCEL) # or Destroy() or Close(True)
And one more thing - I use Linux Mint 15, Python 2.7.4, wxPython 2.8.12.1 :)
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import wx
import sys # to get python version
#----------------------------------------------------------------------
class MyFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(600,100))
self.panel = wx.Panel(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.panel.SetSizer(self.sizer)
self.label = wx.StaticText(self.panel, label="Python "+sys.version+"\nwxPython"+wx.version())
self.button1 = wx.Button(self.panel, label="On")
self.button2 = wx.Button(self.panel, label="Off")
self.sizer.Add(self.label)
self.sizer.Add(self.button1)
self.sizer.Add(self.button2)
self.Bind(wx.EVT_BUTTON, self.OpenDialog, self.button1)
self.Bind(wx.EVT_BUTTON, self.CloseDialog, self.button2)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.TimerCloseDialog, self.timer)
self.Show(True)
self.dlg = None
def OpenDialog(self, event):
print "OpenDialog"
self.timer.Start(3000, oneShot=True)
print "wait 3s ..."
if not self.dlg:
self.dlg = wx.DirDialog(self)
self.dlg.ShowModal()
#self.dlg.Show(True)
def CloseDialog(self, event):
print "CloseDialog"
if self.dlg:
#self.dlg = None
#self.dlg.EndModal(wx.CANCEL)
self.dlg.Destroy()
#self.dlg.Close(True)
def TimerCloseDialog(self, event):
print "TimerCloseDialog"
if self.dlg:
#self.dlg = None
self.dlg.EndModal(wx.CANCEL)
#self.dlg.Destroy()
#self.dlg.Close(True)
#----------------------------------------------------------------------
print "Python", sys.version
print "wxPython", wx.version()
app = wx.App()
frame = MyFrame(None, "Hello Dialog")
app.MainLoop()
I have a GUI with two wxNotebook-elements like this:
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"My App",size=(800,600),pos=((wx.DisplaySize()[0]-800)/2,(wx.DisplaySize()[1]-600)/2),style= wx.SYSTEM_MENU | wx.CAPTION | wx.MINIMIZE_BOX | wx.CLOSE_BOX)
self.SetBackgroundColour((232,232,232))
self.p = wx.Panel(self,size=(800,6300),pos=(0,0))
self.SetPages()
def SetPages(self):
self.nb = wx.Notebook(self.p,style=wx.NB_BOTTOM)
page1 = PageOne(self.nb)
page2 = PageTwo(self.nb)
self.nb.AddPage(page1, "page1")
self.nb.AddPage(page2, "page2")
self.sizer = wx.BoxSizer()
self.sizer.Add(self.nb, 1, wx.EXPAND)
self.p.SetSizer(self.sizer)
Now I want to create a third Notebook-page & set focus on it at a certain event. But this does not work:
def CreateNewPageEvent(self, event):
self.CreateNewPage()
def CreateNewPage(self):
page3 = PageThree(self.nb)
self.nb.AddPage(page3, "page3")
I must admit that I'm not sure what a "BoxSizer" does =/
Any ideas to get this working?
Edit: OK, this works for an event inside my MainFrame-class. But I also want to create a new nb-page from an event of another class:
class ContinueApp(MainFrame):
def foo(self):
super(ContinueApp, self).CreateNewPage()
def continueapp(event):
cont = ContinueApp()
cont.foo()
The BoxSizer (and other sizers) are for laying out widgets so you don't have to position them yourself. They also help control which widgets expand or stretch when you make your application window larger or smaller. In your case, you should NOT add the same widget to the same sizer twice. You shouldn't add one widget to two different sizers either.
You need to remove this:
self.nb.AddPage(page1, "page3")
self.sizer.Add(self.nb, 1, wx.EXPAND)
self.p.SetSizer(self.sizer)
Also note that you are adding page1 to the notebook again when you should be adding page3:
page3 = PageThree(self.nb)
self.nb.AddPage(page3, "page3")
If you want to switch between tabs programmatically, you should use the notebook's SetSelection method. I have an example app you can look at in the following tutorial (or the answer below it):
http://www.blog.pythonlibrary.org/2012/07/18/wxpython-how-to-programmatically-change-wx-notebook-pages/
wxpython: How to make a tab active once it is opened via an event handler?
Once you have switched tabs, you may want to set the focus on a widget within that tab. I find that using pubsub to send events is probably the cleanest way to communicate between classes. I have a couple of tutorials on that subject:
For early versions of wxPython 2.8 - http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/
For later versions of wxPython 2.8 and all of 2.9 - http://www.blog.pythonlibrary.org/2013/09/05/wxpython-2-9-and-the-newer-pubsub-api-a-simple-tutorial/
This should help you, just click "GoTo Blue Panel" button.
import wx
import wx.lib
import wx.lib.flatnotebook as FNB
class MyFlatNotebook(FNB.FlatNotebook):
def __init__(self, parent):
mystyle = FNB.FNB_DROPDOWN_TABS_LIST|\
FNB.FNB_FF2|\
FNB.FNB_SMART_TABS|\
FNB.FNB_X_ON_TAB
super(MyFlatNotebook, self).__init__(parent, style=mystyle)
# Attributes
self.textctrl = wx.TextCtrl(self, value="edit me", style=wx.TE_MULTILINE)
self.blue = wx.Panel(self)
self.blue.SetBackgroundColour(wx.BLUE)
# Setup
self.AddPage(self.textctrl, "Text Editor")
self.AddPage(self.blue, "Blue Panel")
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
# Make some buttons
vbox = wx.BoxSizer(wx.VERTICAL)
hbox = wx.BoxSizer(wx.HORIZONTAL)
button = wx.Button(self, wx.ID_OK, "GoTo Blue Panel")
self.Bind(wx.EVT_BUTTON, self.OnButton, button)
hbox.Add(button, 0, wx.ALL, 5)
self.nb = MyFlatNotebook(self)
vbox.Add(hbox, 0, wx.EXPAND)
vbox.Add(self.nb, 1, wx.EXPAND)
self.SetSizer(vbox)
def OnButton(self, event):
self.nb.SetSelection(1)
if __name__=='__main__':
app = wx.App(False)
frame = MyFrame(None, -1, "NoteTest")
frame.Show()
app.MainLoop()
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.