In the following sample program I can't seem to get the notebook control to use the styles I tell it to use. Despite the FNB_HIDE_ON_SINGLE_TAB, FNB_NO_NAV_BUTTONS and FNB_X_ON_TABS styles I still see the navigation buttons and a single tab with no close button. It's the same for all styles I've tried, they're all completely ignored.
I'm running this on Ubuntu 11.10, python 2.7.2 and wxPython 2.8.11 if it matters
import wx
import wx.lib.flatnotebook as FNB
class TabCtrl(FNB.FlatNotebook):
def __init__(self, parent):
windowstyle = FNB.FNB_HIDE_ON_SINGLE_TAB|FNB.FNB_NO_NAV_BUTTONS|FNB.FNB_X_ON_TAB
super(TabCtrl, self).__init__(parent, style=windowstyle)
self.pages = []
self.pages.append(wx.Panel(self))
self.AddPage(self.pages[0], 'Test')
class MainFrame(wx.Frame):
def __init__(self, *args, **kwargs):
super(MainFrame, self).__init__(*args, **kwargs)
self.panel = wx.Panel(self)
self.tabs = TabCtrl(self.panel)
sizer = wx.GridBagSizer(vgap=8, hgap=8)
sizer.Add(self.tabs, (0, 0), (10, 30), wx.EXPAND)
sizer.AddGrowableCol(29)
sizer.AddGrowableRow(9)
self.panel.SetSizer(sizer)
self.panel.Fit()
self.SetInitialSize()
class App(wx.App):
def __init__(self, *args, **kwargs):
super(App, self).__init__(*args, **kwargs)
self.frame = MainFrame(None, title='Test')
self.frame.Show()
app = App()
app.MainLoop()
FNB_NO_NAV_BUTTONS and FNB_X_ON_TABS both work on my machine (using the wxPython demo) using wxPython 2.8.12, Windows 7, Python 2.6. When it comes to anything in the AGW widget set, I highly recommend downloading the latest version from SVN. I know the author of that library and he's always updating it and fixing bugs, so updating to the latest in SVN is usually worth it.
EDIT: I was looking at this again and I think you're putting at least some of those flags in on the wrong parameter. There's an "agwStyle" argument in FlatNotebook too and I'm thinking that's where some or all of those FlatNotebook-related flags should be applied to:
super(TabCtrl, self).__init__(parent, agwStyle=windowstyle)
It may be a bug. I have tried various ways to change the style without being successful. Even example located on wxPyWiki does not work for me when it comes to changing the styles.
I would consider asking about it on wxPython-users mailing list.
Related
I am following this example https://github.com/cztomczak/cefpython/blob/master/examples/wxpython.py for cefpython3 and wxpython. However I am looking to disable the windows border so the program can no longer be adjusted size wise and their is no title bar. But I would like to keep it so the program can be moved around the screen. I have done some research but not come across a way to successfully do this.
The following appears to do the job on Linux.
Your mileage may vary.
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, id=wx.ID_ANY, title="", size=(360,100)):
super(MyFrame, self).__init__(parent, id, title, size, style = wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX))
self.panel = wx.Panel(self)
self.panel.SetBackgroundColour('palegreen')
self.Show()
if __name__ == "__main__":
app = wx.App()
frame = MyFrame(None,title="Non-Resizeable Frame")
app.MainLoop()
How do you define a destructor for a wx.Panel in wxpython?
META:
After inheriting a code base which uses wxpython and PyPubSub I've discovered a huge number of pubsub subscriptions in the __init__ functions of wx.Panel's that are never unsubscribed and cause errors later on in the program.
You should be able to bind to EVT_WINDOW_DESTROY and do the unsub in the handler.
For example:
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, wx.NewId())
pub.subscribe(self.__handler, 'event')
def __destroy(_):
pub.unsubscribe(self.__handler, 'event')
self.Bind(wx.EVT_WINDOW_DESTROY, __destroy)
If above is not working you can protect against the PyDeadObjectError exception, by adding the following in the code where you try to access ExtendedWxPanel:
if instanceOfExctendedWxPanel:
then access it or methods of it.
I had the same issue, I solved it by doing the following:
My code (which gave the error) was:
import wx
from pubsub import pub
class My_panel(wx.panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.box = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self.box)
#
pub.subscribe(self.do_something, 'Msg')
def do_something(self):
self.box.Layout()
The above panel was in a wx.Notebook page. In my app the user has the possibility to add or delete page from this notebook.
When the pub.sendMessage('Msg') code line was run after that the user deleted the notebook page containing this panel, I had the following error:
RuntimeError: wrapped C/C++ object of type BoxSizer has been deleted
which seems to be ~~ the new error type of 'wx.PyDeadObjectError exception' according to wxPython: https://wxpython.org/Phoenix/docs/html/MigrationGuide.html
What is explained in such documentation from wxPython is to use the nonzero() method which tests if the C++ object has been deleted.
Hence my working code is:
import wx
from pubsub import pub
class My_panel(wx.panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.box = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self.box)
#
pub.subscribe(self.do_something, 'Msg')
def do_something(self):
if self.__nonzero__():
self.box.Layout()
On Windows 7 64-bit, using Python 2.7.3 and wx version 2.8.12.1.
I'm trying to get a wx.Frame styled as wx.SIMPLE_BORDER or wx.NO_BORDER to have a shadow like other windows, like this:
Drop Shadow
Unfortunately, this seems to be a part of the wx.RESIZE_BORDER style. Here is what happens without that:
No Shadow
So, I went to Googling and found this ArtManager class in the wx.lib.agw.artmanager module, which had a DropShadow function, claiming that it "Adds a shadow under the window (Windows Only)."
But, it didn't seem to do anything. No errors, but no results. Perhaps it doesn't work on Windows 7, but I'm hoping you have a better answer. Of course, I'd be open to other methods of creating a shadow, but this is the one I had found.
Here's my code:
import wx
import wx.lib.agw.artmanager
class main(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, style=wx.SIMPLE_BORDER, size=(469, 400))
am = wx.lib.agw.artmanager.ArtManager()
self.Center()
self.Show()
am.DropShadow(self, True)
class app(wx.App):
def OnInit(self):
frame = main(None, -1)
frame.Show(True)
return True
app = app()
app.MainLoop()
I am designing a windows utility software for Windows 7 coded in Python with Wxpython for GUI works.I dont want to open my software if it is already opened.
I want a function like this if user opens that software a message box is to be displayed on windows screen showing that "Your application is already running".
Plz help.
Thanks in advance...
There's already existing wxPython facility that implements wanted logic, called wx.SingleInstanceChecker. Here's and example of code (shamelessly borrowed from wxPython wiki):
import wx
class SingleAppFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(300, 300))
self.Centre()
class SingleApp(wx.App):
def OnInit(self):
self.name = "SingleApp-%s" % wx.GetUserId()
self.instance = wx.SingleInstanceChecker(self.name)
if self.instance.IsAnotherRunning():
wx.MessageBox("Another instance is running", "ERROR")
return False
frame = SingleAppFrame(None, "SingleApp")
frame.Show()
return True
app = SingleApp(redirect=False)
app.MainLoop()
This cannonical example (for a matter of luck) makes exatly what you've asked.
Is there anyway to disable a notebook tab? Like you can with the Widgets themselves? I have a long process I kick off, and while it should be pretty self-explanatory for those looking at it, I want to be able to prevent the user from mucking around in other tabs until the process it is running is complete.
I couldn't seem to find anything in wx.Notebook to help with this?
Code snippet:
def __init__(self, parent):
wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=wx.BK_DEFAULT)
self.AddPage(launchTab.LaunchPanel(self), "Launch")
self.AddPage(scanTab.ScanPanel(self), "Scan")
self.AddPage(extractTab.ExtractPanel(self), "Extract")
self.AddPage(virtualsTab.VirtualsPanel(self), "Virtuals")
It si not doable with wx.Notebook. But you can use some of the more advanced widgets such as wx.lib.agw.aui.AuiNotebook:
import wx
import wx.lib.agw.aui as aui
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
style = aui.AUI_NB_DEFAULT_STYLE ^ aui.AUI_NB_CLOSE_ON_ACTIVE_TAB
self.notebook = aui.AuiNotebook(self, agwStyle=style)
self.panel1 = wx.Panel(self.notebook)
self.panel2 = wx.Panel(self.notebook)
self.panel3 = wx.Panel(self.notebook)
self.notebook.AddPage(self.panel1, "First")
self.notebook.AddPage(self.panel2, "Second")
self.notebook.AddPage(self.panel3, "Third")
self.notebook.EnableTab(1, False)
self.Show()
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
Technically, wx.Notebook doesn't have a way to disable a tab. However, you can do the same thing by checking which tab is clicked on and if it is "disabled", veto the EVT_NOTEBOOK_PAGE_CHANGING or EVT_NOTEBOOK_PAGE_CHANGED event. Alternately, you can use the AUI notebook as mentioned above. Note that that is the one from the agw lib, NOT from the wx.aui one. The FlatNotebook also provides the ability to disable tabs. See the wxPython demo for an example.
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
p = wx.Panel(self)
self.nb = wx.Notebook(p)
......
self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
def OnPageChanged(self, event):
if wx.IsBusy():
self.Unbind(wx.EVT_NOTEBOOK_PAGE_CHANGED)
self.nb.SetSelection(event.GetOldSelection())
self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)
The active tab can be set by Notebook.SetSelection(). But the event should be unbinded/disable and bind/enable around it to avoid in infinite looping. There should be wx.BeginBusyCursor(), wx.EndBusyCursor() in panel codes. Then The tab changing is "disabled" when app is busy.