Search code examples
pythonmacoswxpythonstatusbar

wxpython SetStatusText on StatusBar doesn't work


I'm training on wxpython by making a window with a menu bar and a status bar. I'm on mac os so maybe that it works differently because I don't know what is the problem with my code, but I didn't find anything on the Internet.

Here is my code :

#!/usr/bin/python
# coding: utf-8

import wx

class Menus(wx.Frame):
    def __init__(self, ptitle):
        wx.Frame.__init__(self, None, 1, title = ptitle, size = (500, 300))

        menuFile = wx.Menu()
        menuFile.Append(wx.ID_OPEN, "&Open\tCTRL+o")
        menuFile.Append(wx.ID_CLOSE, "&Close\tCTRL+c")
        menuFile.AppendSeparator()
        menuFile.Append(wx.ID_EXIT, "&Quit\tCTRL+q")

        menuBar = wx.MenuBar()
        menuBar.Append(menuFile, "&File")

        self.SetMenuBar(menuBar)

        self.bar = wx.StatusBar(self, 1)
        self.bar.SetFieldsCount(2)
        self.bar.SetStatusWidths([1,1])
        self.SetStatusBar(self.bar)

        self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
        self.Bind(wx.EVT_MENU, self.OnOpen, id=wx.ID_OPEN)
        self.Bind(wx.EVT_MENU, self.OnClose, id=wx.ID_CLOSE)

    def OnOpen(self, evt):
        self.bar.SetStatusText("Choice -> open", 1)

    def OnClose(self, evt):
        self.bar.SetStatusText("Choice -> close", 1)

    def OnExit(self, evt):
        self.Destroy()

class App(wx.App):
    def OnInit(self):
        window = Menus("Window with menu")
        window.Show(True)
        self.SetTopWindow(window)
        return True

app = App()
app.MainLoop()

When I click on Open or Close, there is no text on the status bar, but there is the status bar. If I choose instead of setting the status text to print something in the terminal, it works fine. I also tried tu write self.bar.SetStatusText("Text") and it doesn't work neither.

It would be great if someone knows where is the problem with this status bar.

Thank you


Solution

  • With self.bar.SetStatusWidths([1,1]) you are setting the widths to 1 pixel and 1 pixel wide respectively.
    You should use [-1,-1] (equal), [-1,-2] (part 2 twice as large as part 1) etc
    or use fixed widths [150,200] for example.

    There are two types of fields: fixed widths and variable width fields. For the fixed width fields you should specify their (constant) width in pixels. For the variable width fields, specify a negative number which indicates how the field should expand: the space left for all variable width fields is divided between them according to the absolute value of this number. A variable width field with width of -2 gets twice as much of it as a field with width -1 and so on.

    For example, to create one fixed width field of width 100 in the right part of the status bar and two more fields which get 66% and 33% of the remaining space correspondingly, you should use an array containing -2, -1 and 100.

    The "Close current document" message, I suspect, is internal, very much like the automatic icon added to the menu.

    Finally, self.bar.SetStatusText("Text") should read self.bar.SetStatusText("Text",1) where the 1 is the index of the status bar field that you want the text to be displayed in.

    e.g.

    self.bar = wx.StatusBar(self, 1)
    self.bar.SetFieldsCount(3)
    self.bar.SetStatusWidths([200,-1,-2])
    self.SetStatusBar(self.bar)
    self.bar.SetStatusText("Second position",1)
    self.bar.SetStatusText("Third position",2)
    

    enter image description here