Is a toolbar icon supposed to show? - python

I have the following code and I'm on OSX. However, I'm expecting to see a toolbar icon but I'm not seeing one. Am I doing something wrong or should it work on Windows? Here's the code
import wx
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title,size=(400, 350))
self.InitUI()
self.Centre()
self.Show()
def InitUI(self):
self.panel = wx.Panel(self)
toolbar = wx.ToolBar(self, size=(-1, 128))
toolbar.SetToolBitmapSize((128,128))
bmp2 = wx.ArtProvider.GetBitmap(wx.ART_ADD_BOOKMARK, wx.ART_OTHER, (128,128))
toolbar.AddLabelTool(-1, label="Add", bitmap=bmp2,
shortHelp="Add", kind=wx.ITEM_NORMAL)
toolbar.Realize()
self.SetToolBar(toolbar)
if __name__ == '__main__':
app = wx.App()
Example(None, title='')
app.MainLoop()
Thanks

The call to Realize needs to happen after the SetToolBar. This is because there are two different kinds of toolbars on OSX and which is chosen depends on if it is attached to a frame or not, and all that happens in the Realize call. Also, OSX is picky about the size of the tools, and the 128 you use will likely be reduced to a supported size.

Related

How to prevent user dragging window/frame

I need to create a simple app that cannot be resized, minimised, maximised and moved. I've been able to fulfill all this requirements but one thanks to this answer but I cant find how to prevent window dragging.
I've tried to adapt this C++ answer : when move event is trigerred, I just move back the window to its original position, but it's not very clean : it makes the windows shaky and sometimes minimize all other running apps. Also, the "Move" options is style available from system menu and I'd like to disable it.
So how can I disable dragging the window when clicking on title bar and disable Move option from system menu ?
I'm running Windows 10, Python 3.10 and wxpython 4.1.1.
def __init__(self):
# stuffs ...
self.Center()
self.Show()
x, y = self.GetPosition()
self.x = x
self.y = y
self.Bind(wx.EVT_MOVE, self.on_move)
def on_move(self, ev):
ev.Skip()
self.Move(self.x, self.y)
I would never recommend having a window that is not moveable but if that is a game changer, in your specific situation, then you could define a window that has no frame and thus is not moveable.
The downside is that you would have to include any caption and a close facilty within the window yourself.
I'd also recommend making it Always_on_top.
As you can see, you gain the restrictions but lose compatability with other windows on the desktop.
import wx
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, ("Moveable or Stuck"), size=(420, 210), \
style = wx.FRAME_NO_TASKBAR \
& ~(wx.MINIMIZE_BOX|wx.RESIZE_BORDER|wx.MAXIMIZE_BOX))
panel = wx.Panel(self)
caption = wx.StaticText(panel, -1, "Moveable or Stuck", pos=(10,5))
caption.SetBackgroundColour('lightgrey')
self.CloseButton = wx.BitmapButton(panel, bitmap=wx.ArtProvider.GetBitmap(wx.ART_CLOSE), \
pos=(380,5), size=(32,32))
self.Bind(wx.EVT_BUTTON, self.OnExit)
self.SetWindowStyle(wx.STAY_ON_TOP | wx.BORDER_NONE | wx.FRAME_NO_TASKBAR )
def OnExit(self, event):
self.Destroy()
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()
I don't run windows OS so I can't test the wx.EVT_MOVE_END event, which should give you a better result.
The style gives you just a Close box and prevents the window being included in the taskbar.
Overriding the Move ability provided by the OS, is I believe, not possible, at an App level at least.
import wx
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, ("Moveable or Stuck"), size=(420, 210), \
style = wx.SYSTEM_MENU|wx.CLOSE_BOX|wx.FRAME_NO_TASKBAR \
& ~(wx.MINIMIZE_BOX|wx.RESIZE_BORDER|wx.MAXIMIZE_BOX))
self.Pos = self.GetPosition()
# other than Windows OS
self.Bind(wx.EVT_MOVE, self.OnMove)
# Windows Only
#self.Bind(wx.EVT_MOVE_END, self.OnMove)
def OnMove(self, event):
self.SetPosition(self.Pos)
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()

wxPython - A side toolbar

We all know that the wxPython toolbar is most of the time, if not always placed at the top. But is there a way to have it on the side (left preferably)? Is there a way to turn this code's (taken from here) top toolbar into a side toolbar:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ZetCode wxPython tutorial
This example creates a simple toolbar.
author: Jan Bodnar
website: www.zetcode.com
last modified: April 2018
"""
import wx
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
toolbar = self.CreateToolBar()
qtool = toolbar.AddTool(wx.ID_ANY, 'Quit', wx.Bitmap('texit.png'))
toolbar.Realize()
self.Bind(wx.EVT_TOOL, self.OnQuit, qtool)
self.SetSize((350, 250))
self.SetTitle('Simple toolbar')
self.Centre()
def OnQuit(self, e):
self.Close()
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
Make use of the toolbar Style attributes.
import wx
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
toolbar = self.CreateToolBar(wx.TB_VERTICAL|wx.TB_TEXT)
atool = toolbar.AddTool(wx.ID_ANY, 'Tool_A', wx.Bitmap('stop.png'))
btool = toolbar.AddTool(wx.ID_ANY, 'Tool_B', wx.Bitmap('stop.png'))
ctool = toolbar.AddTool(wx.ID_ANY, 'Quit', wx.Bitmap('stop.png'))
toolbar.Realize()
self.Bind(wx.EVT_TOOL, self.OnQuit, ctool)
self.SetSize((350, 250))
self.SetTitle('Simple toolbar')
self.Centre()
def OnQuit(self, e):
self.Close()
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
Styles available:
wx.TB_FLAT: Gives the toolbar a flat look (Windows and GTK only).
wx.TB_DOCKABLE: Makes the toolbar floatable and dockable (GTK only).
wx.TB_HORIZONTAL: Specifies horizontal layout (default).
wx.TB_VERTICAL: Specifies vertical layout.
wx.TB_TEXT: Shows the text in the toolbar buttons; by default only icons are shown.
wx.TB_NOICONS: Specifies no icons in the toolbar buttons; by default they are shown.
wx.TB_NODIVIDER: Specifies no divider (border) above the toolbar (Windows only)
wx.TB_NOALIGN: Specifies no alignment with the parent window (Windows only, not very useful).
wx.TB_HORZ_LAYOUT: Shows the text and the icons alongside, not vertically stacked (Windows and GTK 2 only). This style must be used with TB_TEXT .
wx.TB_HORZ_TEXT: Combination of TB_HORZ_LAYOUT and TB_TEXT .
wx.TB_NO_TOOLTIPS: Don’t show the short help tooltips for the tools when the mouse hovers over them.
wx.TB_BOTTOM: Align the toolbar at the bottom of parent window.
wx.TB_RIGHT: Align the toolbar at the right side of parent window.
wx.TB_DEFAULT_STYLE: Combination of TB_HORIZONTAL and TB_FLAT . This style is new since wxWidgets 2.9.5.

How to add hyperlinks in the help of a wxpython app?

I'm implementing the help menu of an App done in wxPython. By now, I'm using a txt file opened in a frame. I would like to have hyperlinks in the help text in order to open other txt files in the same frame. However, I don't know how to do this. I don't even know if this is the most elegant way to implement a help menu. Any suggestion will be very useful.
Below you can find part of the code I'm using (you will need a txt file called "Help_Main_App.txt"):
import wx
class Help_Frame(wx.Frame):
title = "Help, I need somebody, help..."
def __init__(self):
wx.Frame.__init__(self, wx.GetApp().TopWindow, title=self.title, size=(450,500))
self.CreateStatusBar()
panel = wx.Panel(self, wx.ID_ANY)
panel.SetBackgroundColour('#ededed')
self.Centre()
vBox = wx.BoxSizer(wx.VERTICAL)
hBox = wx.BoxSizer(wx.HORIZONTAL)
self.textbox = wx.TextCtrl(panel, style=wx.TE_MULTILINE, size=(-1, 295))
hBox.Add(self.textbox, 1, flag=wx.EXPAND)
vBox.Add(hBox, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=10)
panel.SetSizer(hBox)
defaultdir, filename = './', 'Help_Main_App.txt'
self.filePath = '/'.join((defaultdir, filename))
self.textbox.LoadFile(self.filePath)
self.textbox.Disable()
class Main_Window(wx.Frame):
def __init__(self, parent, title):
#wx.Frame.__init__(self, parent, title = title, pos = (0, 0), size = wx.DisplaySize())
wx.Frame.__init__(self, parent, title=title, size=(1000,780))
self.Center()
# Setting up the menu.
filemenu = wx.Menu()
helpmenu = wx.Menu()
menuExit = filemenu.Append(wx.ID_EXIT,"&Exit"," Close window and exit program")
menuHelp = helpmenu.Append(wx.ID_HELP, "&Help"," Help of this program")
# Creating the menubar.
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
menuBar.Append(helpmenu,"&Help") # Adding the "helpmenu" to the MenuBar
self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.
# Set event handlers
self.Bind(wx.EVT_MENU, self.OnHelp, menuHelp)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
def OnHelp(self,e):
Help_Frame().Show()
def OnExit(self,e):
self.Close(True) # Close the frame.
def main():
app = wx.App(False)
frame = Main_Window(None, "Main App")
frame.Show()
app.MainLoop()
if __name__ == "__main__" :
main()
I recommend using an HTMLWindow for something simple like that. It can handle only simple HTML, so don't try to make a website with it as HTMLWindow doesn't support CSS or javascript.
I wrote a simple About box using it. You can read about it here:
http://www.blog.pythonlibrary.org/2008/06/11/wxpython-creating-an-about-box/
The basic idea is to subclass HTMLWindow and override its OnLinkClicked method. Then you can use Python's webbrowser to open the user's default browser. Or you can try using subprocess, although that will be a lot less likely to work unless you always know what is installed on your target machines.
Further to Mikes answer if you are able to use wxPython 2.9.4 or above you can consider using the more advanced html2 webview which does support CSS and javascript. Using this you could make the help as a simple website that can be viewed in program.
http://wxpython.org/Phoenix/docs/html/html2.WebView.html
Its also worth mentioning that if (for some strange reason) you don't want to work with you could achieve a similar outcome with a StyledTxtCtrl.
Late to the party but just for the sake of completeness (seeing that the OP's code was using wx.TextCtrl to show the help text), here is an example on how to add and launch hyperlinks using wx.TextCtrl (I have attached any explanations on the code comments):
class HelpDialog(wx.Dialog):
"""Help Dialog."""
def __init__(self, parent, title, style):
"""Init."""
wx.Dialog.__init__(self, parent=parent, id=wx.ID_ANY,
title=title, pos=wx.DefaultPosition, size=wx.DefaultSize, style=style)
# We need the 'wx.TE_AUTO_URL' style set.
self.help = wx.TextCtrl(self, wx.ID_ANY, '', DPOS, DSIZE,
wx.TE_AUTO_URL|wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2|wx.TE_WORDWRAP)
# Events - this is the interesting part,
# we catch the mouse on hovering the hyperlink:
self.help.Bind(wx.EVT_TEXT_URL, self.openHlpUrl)
# Show dialog
self.ShowModal()
def openHlpUrl(self, event):
"""Open help URL."""
# We get the starting and ending points on
# the text stored in our ctrl from this event
# and we slice it:
url = self.help.GetValue()[event.GetURLStart():event.GetURLEnd()]
# We want to capture the left up mouse event
# when hovering on the hyperlink:
if event.MouseEvent.LeftDown():
# Let's be wxpythion native and launch the browser this way:
wx.LaunchDefaultBrowser(url)

Restoring windows in wxpython

I am having a hard time restoring a window after it has been minimized.
Minimize works fine, but i am trying to open the window back up.. self restores but Vodka_Frame doesn't.
Here is my code:
def minimizeProgram(event):
self.Iconize()
Vodka_Frame.Iconize()
def maximizeProgram(event):
if self.IsIconized()=='True' or Vodka_Frame.IsIconized()=='True':
self.Iconize(False)
Vodka_Frame.Iconize(False)
self.Show(True)
Vodka_Frame.Show(True)
self.Raise()
Vodka_Frame.Raise()
#### Catch the minimize event and minimize both windows.
self.Bind(wx.EVT_ICONIZE,minimizeProgram)
#### Catch the maximize event and maximize both windows.
self.Bind(wx.EVT_LEFT_DCLICK,maximizeProgram)
What am i doing wrong? How can i get my windows back! :)
I'm not sure what you're doing wrong without a small runnable example. However, I created the following simple script that works for me:
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, parent=None, title="Test")
panel = MyPanel(self)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.toggleIconize, self.timer)
self.timer.Start(5000)
self.Show()
#----------------------------------------------------------------------
def toggleIconize(self, event):
""""""
if self.IsIconized() == True:
print "raising..."
self.Iconize(False)
self.Raise()
else:
print "minimizing!"
self.Iconize()
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
Basically it just minimizes and raises itself every 5 seconds. I am using Python 2.6.6 and wxPython 2.8.12.1 on Windows 7 Pro.
The relationship between your frames is not clear, but if you make the other frame child of the main one (i.e. specify the main frame as its parent when creating it), then it will be minimized and restored automatically when the main frame is minimized or restored, without you having to do anything special.

Why is wxGridSizer much slower to initialize on a wxDialog then on a wxFrame?

It seems that this is specific to windows, here is an example that reproduces the effect:
import wx
def makegrid(window):
grid = wx.GridSizer(24, 10, 1, 1)
window.SetSizer(grid)
for i in xrange(240):
cell = wx.Panel(window)
cell.SetBackgroundColour(wx.Color(i, i, i))
grid.Add(cell, flag=wx.EXPAND)
class TestFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
makegrid(self)
class TestDialog(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
makegrid(self)
class Test(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
btn1 = wx.Button(self, label="Show Frame")
btn2 = wx.Button(self, label="Show Dialog")
sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(sizer)
sizer.Add(btn1, flag=wx.EXPAND)
sizer.Add(btn2, flag=wx.EXPAND)
btn1.Bind(wx.EVT_BUTTON, self.OnShowFrame)
btn2.Bind(wx.EVT_BUTTON, self.OnShowDialog)
def OnShowFrame(self, event):
TestFrame(self).Show()
def OnShowDialog(self, event):
TestDialog(self).ShowModal()
app = wx.PySimpleApp()
app.TopWindow = Test()
app.TopWindow.Show()
app.MainLoop()
I have tried this on the following configurations:
Windows 7 with Python 2.5.4 and wxPython 2.8.10.1
Windows XP with Python 2.5.2 and wxPython 2.8.7.1
Windows XP with Python 2.6.0 and wxPython 2.8.9.1
Ubuntu 9.04 with Python 2.6.2 and wxPython 2.8.9.1
The wxDialog wasn't slow only on Ubuntu.
I got a reply on the wxPython-users mailing list, the problem can be fixed by calling Layout explicitly before the dialog is shown.
This is really weird...
My guess is that this is due to
Windows and wxWidgets not dealing very
well with overlapping siblings, and so
when the sizer is doing the initial
layout and moving all the panels from
(0,0) to where they need to be that
something about the dialog is causing
all of them to be refreshed and
repainted at each move. If you
instead do the initial layout before
the dialog is shown then it is just as
fast as the frame.
You can do this by adding a call to window.Layout() at the end of
makegrid.
-- Robin Dunn

Categories