Python Forum
what does self.classname() do and how is it done?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what does self.classname() do and how is it done?
#3
@programmy when dealing with a class self is an internal handle to the class -- in other languages you will find this being used for the handle to the self. Now in PyQt the QMainWindow is the only object that comes with a predefined MenuBar and StatusBar but one can create a window with a simple QWidget and unless one needs the MenuBar and/or StatusBar or the Docking region there is no need to use the QMainWindow with all its extra baggage and I am pretty sure this goes for wxPython as well -- here is a PyQt example with some universal comments to maybe help you along in the right direction
# Okay I am assuming PyQt since you did not denote otherwise
class Form(QMainWindow):
    def __init__(self):
# Do not use super unless you fully understand what the pitfalls are
# because frankly the benefits are more rare than the pitfalls are and
# the whole basis for using it is based on lazy coding practices rather
# than anything of quality
       # ensure the parent's __init__ is called
       QMainWindow.__init__(self)
 
# Without the "self." handle this Panel will get  collected by the
# garbage collector and become no more also due to this being a snippet
# there is no definition for this "wx" prefix for even in PyQt you have
# have to import the libraries and I am assuming you did the same with
# wxPython
       # create a panel in the frame
       self.pnl = wx.Panel(self)
 
       # put some text on the panel
       st = wx.StaticText(self.pnl, label="Hello World!")

       # change the text to have a larger bold font
       font = st.GetFont()
       font.PointSize += 10
       font = font.Bold()
       st.SetFont(font)
 
       # create a sizer to manage the layout of child widgets
       sizer = wx.BoxSizer(wx.VERTICAL)
       sizer.Add(st, wx.SizerFlags().Border(wx.TOP|wx.LEFT, 25))
       self.pnl.SetSizer(sizer)
 
       # create a menu bar
# this is a meaningless menubar because you have nothing in it
       self.makeMenuBar()
 
       # and a status bar
       self.CreateStatusBar()
# okay so this wx prefix has something to do with the library you are using 
# that I am guessing you have imported incorrectly because you imported the
# entire library rather than just the elements you are using from that library
       self.SetStatusText("Welcome to wxPython!")
Reply


Messages In This Thread
RE: what does self.classname() do and how is it done? - by Denni - Oct-22-2019, 03:09 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020