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()
Related
I want to add a few submenu items to an existing popup menu item. I found several examples on how to create a submenu for a standard menu, but none about creating a submenu for a popup menu.
I tried to apply to the popup menu the same logic as for a standard menu, but failed to do that properly (while at the same time I have no trouble in creating a standard menu with submenus).
In a portion of code like the one below, how to add a submenu ?
# ...
class ButtonContext(wx.Menu):
def __init__(self, parent):
super(ButtonContext, self).__init__()
self.parent = parent
button_popup = wx.MenuItem(self, wx.ID_ANY, 'test popup')
self.Append(button_popup)
self.Bind(wx.EVT_MENU, self.button_action, button_popup)
def button_action(self, event):
event.Skip()
# ...
This will work, in fact popup menus are based on wx.Menu class so the logic and the behaviour are the same
class ButtonContext(wx.Menu):
def __init__(self, parent):
super(ButtonContext, self).__init__()
self.parent = parent
button_popup = wx.MenuItem(self, wx.ID_ANY, 'test popup')
submenu_popup = wx.Menu()
submenu_button_popup = wx.MenuItem(self, wx.ID_ANY, 'test submenu popup')
submenu_popup.Append(submenu_button_popup)
self.AppendSubMenu(submenu_popup, "submenu")
self.Append(button_popup)
self.Bind(wx.EVT_MENU, self.button_action, button_popup)
Just to illustrate the SetSubMenu(menu) method which can also be used to create a sub-menu in this instance.
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.SetSize((400, 300))
self.SetTitle("test frame")
sizer_0 = wx.BoxSizer(wx.HORIZONTAL)
self.window_1 = wx.SplitterWindow(self, wx.ID_ANY)
self.window_1.SetMinimumPaneSize(20)
sizer_0.Add(self.window_1, 1, wx.EXPAND, 0)
self.pane_1 = wx.Panel(self.window_1, wx.ID_ANY)
sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
self.pane_2 = wx.Panel(self.window_1, wx.ID_ANY)
sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
self.button = wx.Button(self.pane_2, wx.ID_ANY, "test button")
sizer_2.Add(self.button, 0, 0, 0)
self.pane_1.SetSizer(sizer_1)
self.pane_2.SetSizer(sizer_2)
self.window_1.SplitVertically(self.pane_1, self.pane_2)
self.SetSizer(sizer_0)
self.Layout()
self.Bind(wx.EVT_CONTEXT_MENU, self.OnButtonContextMenu, self.button)
self.Show()
def OnButtonContextMenu(self, event):
self.PopupMenu(ButtonContext(self))
class ButtonContext(wx.Menu):
def __init__(self, parent):
super(ButtonContext, self).__init__()
self.parent = parent
button_popup = wx.MenuItem(self, wx.ID_ANY, 'test popup')
self.Append(button_popup)
submenu = wx.Menu()
submenu1 = wx.MenuItem(submenu, wx.ID_ANY, 'Submenu 1')
submenu2 = wx.MenuItem(submenu, wx.ID_ANY, 'Submenu 2')
submenu3 = wx.MenuItem(submenu, wx.ID_ANY, 'Submenu 3')
submenu.Append(submenu1)
submenu.Append(submenu2)
submenu.Append(submenu3)
button_popup2 = wx.MenuItem(self, wx.ID_ANY, 'A sub menu')
button_popup2.SetSubMenu(submenu)
self.Append(button_popup2)
self.Bind(wx.EVT_MENU, self.button_action, button_popup)
self.Bind(wx.EVT_MENU, self.submenu1_action, submenu1)
def button_action(self, event):
print("Test Menu Button")
event.Skip()
def submenu1_action(self, event):
print("Submenu 1")
event.Skip()
class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(None, wx.ID_ANY, "")
return True
if __name__ == "__main__":
app = MyApp()
app.MainLoop()
There is a simple text editor developed with WxPython.
I need to add hot keys. ctrl+1, ctrl+2, ctrl+3 and ctrl+4. So that when you press these hot keys to perform certain functions.
How can I do it?
The code:
import os
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title)
self.textBox = wx.TextCtrl(self, style=wx.TE_MULTILINE)
fileMenu = wx.Menu()
menuOpen = fileMenu.Append(wx.ID_OPEN,"O&pen","")
menuExit = fileMenu.Append(wx.ID_EXIT,"E&xit","")
menuBar = wx.MenuBar()
menuBar.Append(fileMenu,"&File")
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Show(True)
def OnOpen(self,e):
self.dirName = ''
dlg = wx.FileDialog(self, "Choose a file", self.dirName, "", "text file|*.txt", wx.FD_OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.fileName = dlg.GetFilename()
self.dirName = dlg.GetDirectory()
f = open(os.path.join(self.dirName, self.fileName), 'r')
self.textBox.SetValue(f.read())
f.close()
dlg.Destroy()
def OnExit(self,e):
self.Close(True)
app = wx.App(False)
frame = MyFrame(None, 'RecordBooks')
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()
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