[WxPython] accessing wxwidgets - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [WxPython] accessing wxwidgets (/thread-34844.html) |
accessing wxwidgets - jim - Sep-06-2021 This is something simple that I cannot get my head around! In one module I set up a wx gui, that has some buttons which are bound to events, and some like wxlabel which are not. this is imported into a second module, within which the attributes for the widgets are manipulated eg "button_1.Hide()" This works fine for the widgets that are bound to events, but not if that is not the case eg label_1.Hide(). I am pretty new at this and have tried everything to acheive this unsuccessfully #first module first.py import wx class MyFrame(wx.Frame): def __init__(self, *args, **kwds): # begin wxGlade: MyFrame.__init__ kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) self.SetSize((400, 300)) self.SetTitle("first") self.panel_1 = wx.Panel(self, wx.ID_ANY) label_1 = wx.StaticText(self.panel_1, wx.ID_ANY, "Password:", style=wx.ALIGN_CENTER_HORIZONTAL) label_1.SetMinSize((80, 30)) label_1.SetFont(wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, 0, "")) sizer_1.Add(label_1, 0, wx.LEFT, 50) sizer_1.Add(sizer_4, 1, wx.EXPAND, 0) self.button_1 = wx.Button(self.panel_1, wx.ID_ANY, "hide") self.button_1.SetMinSize((80, 23)) sizer_1.Add(button_1, 1, wx.LEFT, 50) self.Layout() self.Bind(wx.EVT_BUTTON, self.hide, self.button_1) def hide(self, event): # wxGlade: MyFrame.<event_handler> print("Event handler 'hide' not implemented!") event.Skip() class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(None, wx.ID_ANY, "") self.SetTopWindow(self.frame) self.frame.Show() return True # end of class MyApp def __del__( self ): pass -------------------------------------------------------------- # second module "second.py" import wx import first class MyApp(fileHide.MyFrame): def __init__(self,parent): first.MyFrame. __init__(self,parent) def hide(self, event): print('hide') self.button_1.Hide() app = wx.App(False) frame = MyApp(None) frame.Show(True) frame.label_1.Hide() # error no frame has no attribute label_1 .... app.MainLoop()So I want to access label_1 in the second module, how can I do this? Thanks in advance RE: accessing wxwidgets - Yoriz - Sep-06-2021 To enable frame.label_1.Hide() to work, label_1 needs to be an attribute of MyFrame in first.py to do this change label_1 to self.label_1 The code you posted did not run, I tided it up and made it runnable first.py import wx class MyFrame(wx.Frame): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._create_controls() self._setup() self._create_sizers() self._create_binds() def on_button_1(self, event): print("Event handler 'on_button_1' not implemented!") event.Skip() def _create_controls(self): self.panel_1 = wx.Panel(self, wx.ID_ANY) self.label_1 = wx.StaticText( self.panel_1, label="Password:", style=wx.ALIGN_CENTER_HORIZONTAL ) self.button_1 = wx.Button(self.panel_1, label="hide") def _setup(self): self.SetSize((400, 300)) self.SetTitle("first") width, height = 80, 30 self.label_1.SetMinSize((width, height)) self.label_1.SetFont( wx.Font( 12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, 0, "", ) ) width, height = 80, 23 self.button_1.SetMinSize((width, height)) def _create_sizers(self): panel_sizer = wx.BoxSizer(wx.VERTICAL) self.panel_1.SetSizer(panel_sizer) # sizer_1.Add(label_1, 0, wx.LEFT, 50) # sizer_1 not defined # sizer_1.Add(sizer_4, 1, wx.EXPAND, 0) # sizer_1 & sizer_4 not defined # sizer_1.Add(button_1, 1, wx.LEFT, 50) # sizer_1 missing, button_1 not defined # sizer_1 = wx.BoxSizer(orient=wx.VERTICAL) panel_sizer.Add(self.label_1, 0, wx.LEFT, 50) panel_sizer.Add(self.button_1, 1, wx.LEFT, 50) self.panel_1.Layout() def _create_binds(self): # self.Bind(wx.EVT_BUTTON, self.hide, self.button_1) # do the bind directly on the button instance as follows self.button_1.Bind(wx.EVT_BUTTON, self.on_button_1) class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(None) self.SetTopWindow(self.frame) self.frame.Show() return True if __name__ == "__main__": my_app = MyApp(False) my_app.MainLoop()second.py import wx import first # class MyApp(fileHide.MyFrame): # fileHide not defined class MyFrame2(first.MyFrame): def __init__(self, parent): super().__init__(parent) def on_button_1(self, event): print("hide") self.button_1.Hide() if __name__ == "__main__": app = wx.App(False) frame = MyFrame2(None) frame.Show(True) frame.label_1.Hide() app.MainLoop() RE: accessing wxwidgets - jim - Sep-07-2021 Thank you so much! Sorry it did not run, I missed some changes in the simplification. I used wxglade to make the gui and it obviiously didn't put in the "self" for the label. The only bit I do not understand is the "super().__init__(*args, **kwargs)" line, can you tell me what that does? RE: accessing wxwidgets - Yoriz - Sep-07-2021 Python’s super() considered super! Raymond Hettinger - Super considered super! - PyCon 2015 RE: accessing wxwidgets - jim - Sep-08-2021 Thanks! RE: accessing wxwidgets - deanhystad - Sep-11-2021 You can configure labels to store as attribute. Click on the label in the designer and look at the "Common" tab of the "Properties" frame. There you will see a checkbox "Store as attribute". It is not checked by default. |