简体   繁体   中英

Setting Frame size in WxPython?

I'm incredibly new to this, so I apologize if this is blatantly obvious. I'm working with sample code (it runs properly):

class MainWindow(wx.Frame):
          def __init__(self, parent, title):
          self.dirname=''


          # A "-1" in the size parameter instructs wxWidgets to use the default size.
          # In this case, we select 200px width and the default height.
          wx.Frame.__init__(self, parent, title=title, size=(1000,1000))
          self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
          self.CreateStatusBar() # A Statusbar in the bottom of the window

          # Setting up the menu.
          filemenu= wx.Menu()
          menuOpen = filemenu.Append(wx.ID_OPEN, "&Open"," Open a file to edit")
          menuAbout= filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
          menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")

          # Creating the menubar.
          menuBar = wx.MenuBar()
          menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
          self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.

          # Events.
          self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
          self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
          self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)

          self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
          self.buttons = []
          for i in range(0, 6):
              self.buttons.append(wx.Button(self, -1, "Button &"+str(i)))
              self.sizer2.Add(self.buttons[i], 1, wx.EXPAND)

          # Use some sizers to see layout options
          self.sizer = wx.BoxSizer(wx.VERTICAL)
          self.sizer.Add(self.control, 1, wx.EXPAND)
          self.sizer.Add(self.sizer2, 0, wx.EXPAND)

          #Layout sizers
          self.SetSizer(self.sizer)
          self.SetAutoLayout(1)
          self.sizer.Fit(self)
          self.Show()

I thought it would be as simple as changing the "size = " value within wx.Frame. init but when I change that nothing happens. How can I alter the window size?

Additionally, how would I tell the window what location on the screen to open at? Thanks

I think the sizers are changing the size of the window on you - that's what they do, after all. Comment out all the code after and including

self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)

and see if you can change your windows size through the init method.

The problem is actually the fact that you're calling self.sizer.Fit . The Fit method tells wxPython to make the widgets fit in the smallest amount of space possible while still showing all the widgets. I very rarely use this method because it doesn't always do what I expect. You can just comment out that line and then set the size to anything you want.

To set the location of the frame, you will want to call SetPosition and pass it a tuple of screen coordinates. Make sure you call SetPosition before you Show the frame as it will look odd if the frame appears in one location and then teleports to a new one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM