I am learning wxpython following the tutorials on zetcode about menu bars and status bars. Please forgive me if the question is stupid.
The code below from the website works fine, but I am curious how to hide the status bar by default (when the application window popup).
import wx
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
menubar = wx.MenuBar()
fileMenu = wx.Menu()
viewMenu = wx.Menu()
self.shst = viewMenu.Append(wx.ID_ANY, 'Show statubar',
'Show Statusbar', kind=wx.ITEM_CHECK)
self.shtl = viewMenu.Append(wx.ID_ANY, 'Show toolbar',
'Show Toolbar', kind=wx.ITEM_CHECK)
viewMenu.Check(self.shst.GetId(), True)
viewMenu.Check(self.shtl.GetId(), True)
self.Bind(wx.EVT_MENU, self.ToggleStatusBar, self.shst)
self.Bind(wx.EVT_MENU, self.ToggleToolBar, self.shtl)
menubar.Append(fileMenu, '&File')
menubar.Append(viewMenu, '&View')
self.SetMenuBar(menubar)
self.toolbar = self.CreateToolBar()
self.toolbar.AddLabelTool(1, '', wx.Bitmap('texit.png'))
self.toolbar.Realize()
self.statusbar = self.CreateStatusBar()
self.statusbar.SetStatusText('Ready')
self.SetSize((350, 250))
self.SetTitle('Check menu item')
self.Centre()
self.Show(True)
def ToggleStatusBar(self, e):
if self.shst.IsChecked():
self.statusbar.Show()
else:
self.statusbar.Hide()
def ToggleToolBar(self, e):
if self.shtl.IsChecked():
self.toolbar.Show()
else:
self.toolbar.Hide()
def main():
ex = wx.App()
Example(None)
ex.MainLoop()
if __name__ == '__main__':
main()
I have tried to change one line above into:
viewMenu.Check(self.shst.GetId(), False)
Sadly, that didn't work out.
Willing to hear any advice! Thanks in advance!
Specs: wxpython: 2.8.12.1; python 2.7; Ubuntu 12.04
IMHO, because viewMenu.Check(..) is called before UI initialization is done (before event loop begin), it may not fire event.
How about manually call ToggleStatusBar method after viewMenu.Check?
def InitUI(self):
....
viewMenu.Check(self.shst.GetId(), False)
self.ToggleStatusBar(None)
This might be what you mean by Just curious if any built-in functions can do that kind of "update" job?
There is a wx.EVT_UPDATE_UI event, i've modifed your code to use it.
UpdateUIEvent
import wx
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
menubar = wx.MenuBar()
fileMenu = wx.Menu()
viewMenu = wx.Menu()
self.shst = viewMenu.Append(-1, 'Show statubar', 'Show Statusbar',
kind=wx.ITEM_CHECK)
self.shtl = viewMenu.Append(-1, 'Show toolbar', 'Show Toolbar',
kind=wx.ITEM_CHECK)
self.shst.Check(False)
self.shtl.Check(True)
menubar.Append(fileMenu, '&File')
menubar.Append(viewMenu, '&View')
self.SetMenuBar(menubar)
self.toolbar = self.CreateToolBar()
self.toolbar.AddLabelTool(1, '', wx.EmptyBitmap(16, 16))
self.toolbar.Realize()
self.statusbar = self.CreateStatusBar()
self.statusbar.SetStatusText('Ready')
self.Bind(wx.EVT_UPDATE_UI, self.on_update_status_bar, self.statusbar)
self.Bind(wx.EVT_UPDATE_UI, self.on_update_tool_bar, self.toolbar)
self.SetSize((350, 250))
self.SetTitle('Check menu item')
self.Centre()
self.Show(True)
def on_update_status_bar(self, event):
event.Show(self.shst.IsChecked())
def on_update_tool_bar(self, event):
event.Show(self.shtl.IsChecked())
def main():
ex = wx.App()
Example(None)
ex.MainLoop()
if __name__ == '__main__':
main()
I think this way would be better.
statusBarHandle.Hide()
self.SetStatusBar(None) #Disable Status Bar
#Enable
statusBarHandle.Show()
self.SetStatusBar(statusBarHandle) #Back created StatusBar(self.CreateStatusBar function)
#Useful tip: for update all widgets in AUI, use AUI function Update()
#For sizers you need to use the Layout() or Update() function
Related
I am trying to create simple window with menu in wx python 3.0.4.
Actually I get an error:
wx.Frame.init(self, ID_ANY, "Title", DefaultPosition, (350,200), DEFAULT_FRAME_STYLE, FrameNameStr) TypeError: Frame():
arguments did not match any overloaded call: overload 1: too many
arguments overload 2: argument 1 has unexpected type 'StandardID'
Even this code is from documentation. Could someone tell me what am I doing wrong, please?
import wx
from wx import *
class MainWindow(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, ID_ANY, "Title", DefaultPosition, (350,200), DEFAULT_FRAME_STYLE, FrameNameStr)
self.Bind(wx.EVT_CLOSE, self.OnClose)
menuBar = wx.MenuBar()
menu = wx.Menu()
m_exit = menu.Append(wx.ID_EXIT, "E&xit", "Close window and exit program.")
self.Bind(wx.EVT_MENU, self.OnClose, m_exit)
menuBar.Append(menu, "&File")
menu = wx.Menu()
m_about = menu.Append(wx.ID_ABOUT, "&About", "Information about this program")
self.Bind(wx.EVT_MENU, self.OnAbout, m_about)
menuBar.Append(menu, "&Help")
self.SetMenuBar(menuBar)
self.main_panel = MainPanel(self)
def OnClose(self, e):
self.Close()
def OnAbout(self, event):
dlg = AboutBox(self)
dlg.ShowModal()
dlg.Destroy()
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
class AboutBox(wx.MessageDialog):
def __init__(self, parent):
wx.MessageDialog.__init__(parent, "About", "About", wx.OK | wx.ICON_INFORMATION, pos=DefaultPosition)
self.CentreOnParent(wx.BOTH)
self.SetFocus()
if __name__ == "__main__":
app = wx.App(False)
frame = MainWindow()
frame.Show()
app.MainLoop()
Frame(parent, id=ID_ANY, title="", pos=DefaultPosition,
size=DefaultSize, style=DEFAULT_FRAME_STYLE, name=FrameNameStr)
You missed the parent argument.
Working code
import wx
class MainWindow(wx.Frame):
def __init__(self):
wx.Frame.__init__(
self, None, wx.ID_ANY, "Title", wx.DefaultPosition, (350,200),
wx.DEFAULT_FRAME_STYLE, wx.FrameNameStr)
self.Bind(wx.EVT_CLOSE, self.OnClose)
menuBar = wx.MenuBar()
menu = wx.Menu()
m_exit = menu.Append(
wx.ID_EXIT, "E&xit", "Close window and exit program.")
self.Bind(wx.EVT_MENU, self.OnClose, m_exit)
menuBar.Append(menu, "&File")
menu = wx.Menu()
m_about = menu.Append(
wx.ID_ABOUT, "&About", "Information about this program")
self.Bind(wx.EVT_MENU, self.OnAbout, m_about)
menuBar.Append(menu, "&Help")
self.SetMenuBar(menuBar)
self.main_panel = MainPanel(self)
def OnClose(self, e):
self.Destroy()
def OnAbout(self, event):
dlg = AboutBox(self)
dlg.ShowModal()
dlg.Destroy()
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
class AboutBox(wx.MessageDialog):
def __init__(self, parent):
wx.MessageDialog.__init__(
self, parent, "About", "About", wx.OK | wx.ICON_INFORMATION,
pos=wx.DefaultPosition)
self.CentreOnParent(wx.BOTH)
if __name__ == "__main__":
app = wx.App(False)
frame = MainWindow()
frame.Show()
app.MainLoop()
I am making a quick and dirty soundboard using the wxPython package and was wondering how to go by implementing a scroll-list of sounds to play.
Here is a picture of what I am trying to convey:
http://i.imgur.com/av0E5jC.png
and here is my code so far:
import wx
class windowClass(wx.Frame):
def __init__(self, *args, **kwargs):
super(windowClass,self).__init__(*args,**kwargs)
self.basicGUI()
def basicGUI(self):
panel = wx.Panel(self)
menuBar = wx.MenuBar()
fileButton = wx.Menu()
editButton = wx.Menu()
exitItem = fileButton.Append(wx.ID_EXIT, 'Exit','status msg...')
menuBar.Append(fileButton, 'File')
menuBar.Append(editButton, 'Edit')
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.Quit, exitItem)
wx.TextCtrl(panel,pos=(10,10), size=(250,150))
self.SetTitle("Soundboard")
self.Show(True)
def Quit(self, e):
self.Close()
def main():
app = wx.App()
windowClass(None)
app.MainLoop()
main()
My question remains, how does one load a list of sounds on that panel and click a certain button to play that sound. I don't really care about implementing pause and fast forward features since this is only going to play really quick sound files.
Thanks in advance.
Just removed the text widget, replaced by a listbox, and hooked a callback on item click, slightly elaborate: when clicking, it finds the position of the item, retrieves the label name and fetches the filename in the dictionary
(we want to play a .wav file with a path, but not necessarily display the full filename)
I refactored the code so callbacks and other attributes are private, helps the lisibility.
import wx
class windowClass(wx.Frame):
def __init__(self, *args, **kwargs):
super(windowClass,self).__init__(*args,**kwargs)
self.__basicGUI()
def __basicGUI(self):
panel = wx.Panel(self)
menuBar = wx.MenuBar()
fileButton = wx.Menu()
editButton = wx.Menu()
exitItem = fileButton.Append(wx.ID_EXIT, 'Exit','status msg...')
menuBar.Append(fileButton, 'File')
menuBar.Append(editButton, 'Edit')
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.__quit, exitItem)
self.__sound_dict = { "a" : "a.wav","b" : "b.wav","c" : "c2.wav"}
self.__sound_list = sorted(self.__sound_dict.keys())
self.__list = wx.ListBox(panel,pos=(20,20), size=(250,150))
for i in self.__sound_list: self.__list.Append(i)
self.__list.Bind(wx.EVT_LISTBOX,self.__on_click)
#wx.TextCtrl(panel,pos=(10,10), size=(250,150))
self.SetTitle("Soundboard")
self.Show(True)
def __on_click(self,event):
event.Skip()
name = self.__sound_list[self.__list.GetSelection()]
filename = self.__sound_dict[name]
print("now playing %s" % filename)
def __quit(self, e):
self.Close()
def main():
app = wx.App()
windowClass(None)
app.MainLoop()
main()
I am trying to set up modified ALT+key accelerators in wxpython that will open the menus in the menubar, but without pressing the ALT key. Hence, pressing the 'R' key in my example below would in theory open the Run menu, but everything that I have tried so far doesn't work. (I have tried sending custom events to the menu item, to the menubar, etc.) It seems that wx.Menu classes just do not have any sort of Popup or Show methods, which would otherwise be ideal. There is of course PopupMenu, but that can't be used for menus that are in a menubar. And menubar doesn't have any useful methods for popping up a menu that is part of it as far as I can tell
import wx
class MainFrame(wx.Frame):
def __init__(self, parent, *args, **kwargs):
wx.Frame.__init__(self, parent, *args, **kwargs)
self.make_menu_bar()
# INSERT MAGIC CODE HERE TO OPEN THE RUN MENU WITHOUT CLICKING ON IT
def make_menu_bar(self):
self.menuRun = wx.Menu()
self.menuRunGo = wx.MenuItem(self.menuRun, -1, "&Go", "", wx.ITEM_NORMAL)
self.menuRun.AppendItem(self.menuRunGo)
self.menuBar = wx.MenuBar()
self.menuBar.Append(self.menuRun, "&Run")
self.SetMenuBar(self.menuBar)
if __name__=='__main__':
app = wx.App()
frame = MainFrame(None)
frame.Show()
app.MainLoop()
EDIT Here is a snippet trying at this with accelerators
import wx
class MainFrame(wx.Frame):
def __init__(self, parent, *args, **kwargs):
wx.Frame.__init__(self, parent, *args, **kwargs)
self.make_menu_bar()
accelEntries = []
accelEntries.append((wx.ACCEL_NORMAL, ord("R"), self.menuRunId))
accelTable = wx.AcceleratorTable(accelEntries)
self.SetAcceleratorTable(accelTable)
def make_menu_bar(self):
self.menuRun = wx.Menu()
self.menuRunGo = wx.MenuItem(self.menuRun, -1, "&Go", "", wx.ITEM_NORMAL)
self.menuRun.AppendItem(self.menuRunGo)
self.menuBar = wx.MenuBar()
self.menuRunId = self.menuBar.Append(self.menuRun, "&Run")
self.SetMenuBar(self.menuBar)
if __name__=='__main__':
app = wx.App()
frame = MainFrame(None)
frame.Show()
app.MainLoop()
How to open the window inside a frame in wxpython.
I want to open a window on clicking File->SubFile .The code is given below.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
class MainMenu(wx.Frame):
def __init__(self, *args, **kwargs):
# ID_F = 1
super(MainMenu, self).__init__(*args, **kwargs)
self.Maximize(True)
self.InitUI()
def InitUI(self):
#self.Bind(wx.EVT_MENU, self.OnAbout, None,id= 1)
fileMenu1 = wx.Menu()
menubar = wx.MenuBar()
fitem1 = fileMenu1.Append(1, '&Sub File\tCtrl+B', 'Sub File..')
menubar.Append(fileMenu1, '&File')
self.SetMenuBar(menubar)
self.SetTitle('Simple menu')
self.Centre()
self.Show(True)
def OnAbout(self, event):
AboutFrame().Show()
class AboutFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent, 5, 'New Window', size=(400,300))
wx.Frame.CenterOnScreen(self)
def main():
ex = wx.App()
MainMenu(None)
ex.MainLoop()
if __name__ == '__main__':
main()
I thought it would be simple.
How to center the subwindow on the screen.
I'm incredibly new to this,
Note To respond to a menu selection, provide a handler for EVT_MENU
[for] the frame that contains the menu.
http://wxpython.org/Phoenix/docs/html/MenuBar.html#menubar
...
wx.Frame methods:
Center()
http://wxpython.org/Phoenix/docs/html/Frame.html?highlight=frame#Frame.Centre
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
class MainMenu(wx.Frame):
def __init__(self, *args, **kwargs):
super(MainMenu, self).__init__(*args, **kwargs)
self.Maximize(True)
self.InitUI()
def InitUI(self):
fileMenu1 = wx.Menu()
menubar = wx.MenuBar()
#********HERE************
fitem1 = fileMenu1.Append(-1, '&Sub File\tCtrl+B', 'Sub File..')
fitem2 = fileMenu1.Append(-1, '&Dog\tCtrl+D', 'Sub Dog..')
self.Bind(wx.EVT_MENU, self.onclick_subfile, fitem1)
#*************************
menubar.Append(fileMenu1, '&File')
self.SetMenuBar(menubar)
self.SetTitle('Simple menu')
self.Centre()
self.Show(True)
def OnAbout(self, event):
AboutFrame().Show()
#**********HERE*************
def onclick_subfile(self, event):
frame = wx.Frame(None, -1, "My Second Frame")
frame.Center()
frame.Show()
class AboutFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent, 5, 'New Window', size=(400,300))
wx.Frame.CenterOnScreen(self)
def main():
ex = wx.App(redirect=False)
MainMenu(None)
ex.MainLoop()
if __name__ == '__main__':
main()
If you want the inner window to be destroyed when you close the outer window, then make the outer window the parent of the inner window:
def onclick_subfile(self, event):
frame = wx.Frame(self, -1, "My Second Frame")
Yop, all is in title, i wanna Hide my wx.MenuBar() on my soft, and that is works well on my Ubuntu, but when i switch my soft on Windows, my wx.MenuBar() is not hide... Any Ideas?
menuBar = wx.MenuBar()
self.fileMenu = wx.Menu()
i = self.fileMenu.Append(-1, _("Load Model\tCTRL+L"))
self.Bind(wx.EVT_MENU, self.showLoadModel(), i)
menuBar.Append(self.fileMenu, 'File')
self.SetMenuBar(menuBar)
menuBar.Hide()
EDIT: Then how can i catch a CTRL+L without EVT_MENU ?
It's ok i found it:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
class Example(wx.Frame):
def __init__(self, *args, **kw):
super(Example, self).__init__(*args, **kw)
self.InitUI()
def InitUI(self):
pnl = wx.Panel(self)
pnl.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
pnl.SetFocus()
self.SetSize((250, 180))
self.SetTitle('Key event')
self.Centre()
self.Show(True)
def OnKeyDown(self, e):
key = e.GetKeyCode()
if key == wx.WXK_ESCAPE:
ret = wx.MessageBox('Are you sure to quit?', 'Question',
wx.YES_NO | wx.NO_DEFAULT, self)
if ret == wx.YES:
self.Close()
def main():
ex = wx.App()
Example(None)
ex.MainLoop()