wxPython SetMinSize problem

2019-08-29 11:38发布

问题:


Having problems setting the SetMinSize in wxPython. Can someone please help me getting this window not getting any smaller than it is in the startup? And/or making the window not allowing resize?

I tried self.SetMinSize(GetSize()) after self.SetSizerAndFit(SizerH) in the MainPanel class. Did not work.

I've looked and searched but to no help.
Im also new to programming and can someone also comment on how the program is built up? Is it understandable and good? Or should measures be taken?

Appreciate any help. =]

Heres the code:

import wx


ID_EXIT = 110

class MainPanel(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        self.parent = parent

        #------------- Setting up the buttons
        Run = wx.Button(self, label="Run")
        Run.Bind(wx.EVT_BUTTON, self.Run )

        #------------- Setting up Static text
        ChooseRoot = wx.StaticText(self, label ="Root catalog: ")
        ScratchWrk = wx.StaticText(self, label ="Sratch workspace: ")
        MergeFile = wx.StaticText(self, label ="Merge file: ")

        #------------ Setting up inputtext
        ChooseRootTxt = wx.TextCtrl(self, -1, size=(210,-1))

        #------------- Setting up the outputbox
        Output = wx.TextCtrl(self, style=wx.TE_MULTILINE|wx.TE_READONLY)

        #------------- Setting up the sizer
        SizerV1 = wx.BoxSizer(wx.VERTICAL)
        SizerV1.Add(ChooseRoot, 0,wx.ALIGN_RIGHT|wx.ALL, 5)
        SizerV1.Add(ScratchWrk, 0, wx.ALIGN_RIGHT|wx.ALL, 5)
        SizerV1.Add(MergeFile, 0, wx.ALIGN_RIGHT|wx.ALL, 5)

        SizerV3 = wx.BoxSizer(wx.VERTICAL)
        SizerV3.Add(ChooseRootTxt, 0, wx.ALIGN_RIGHT|wx.ALL, 5)

        SizerV2 = wx.BoxSizer(wx.VERTICAL)
        SizerV2.Add(Run, 0, wx.ALIGN_RIGHT|wx.ALL, 5)

        SizerH1 = wx.BoxSizer()
        SizerH1.Add(SizerV1, 0, wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL)
        SizerH1.Add(SizerV3, 1, wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL)
        SizerH1.Add(SizerV2, 0, wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL)

        SizerH2 = wx.BoxSizer()
        SizerH2.Add(Output, 1, wx.EXPAND | wx.ALL, 5)

        SizerH = wx.BoxSizer(wx.VERTICAL)
        SizerH.Add(SizerH1, 0, wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL)
        SizerH.Add(SizerH2, 1, wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL)


        self.SetSizerAndFit(SizerH)

    #--- START EVENT HANDLERS

    def Run(self, event=None):
        pass

class MainWindow(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, wx.ID_ANY, title, size = (415,330),
                          style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE | wx.STAY_ON_TOP)

        self.CreateStatusBar() # Creates statusbar
        #------------- Setting up the menu
        filemenu = wx.Menu()
        filemenu.Append(ID_EXIT, "E&xit", "Exit the program")
        #------------- Creating the menu
        menubar = wx.MenuBar()
        menubar.Append(filemenu, "&File")
        self.SetMenuBar(menubar)
        #---------- Setting menu event handlers
        wx.EVT_MENU(self, ID_EXIT, self.OnExit)                    
        #--- Add MainPanel
        self.Panel = MainPanel(self, -1)

        #Centre on Screen
        self.CentreOnScreen()

        ###---- SHOW THE WINDOW
        self.Show(True)

    def OnExit(self,  event):
        self.Close(True) # Close the Frame
    #--- END EVENT HANDLERS ---------------------------------

if __name__=='__main__':
    try:
        app = wx.PySimpleApp()
        frame = MainWindow(None, -1, "Indexinator3000")
        app.MainLoop()
    finally:
        del app

回答1:

Fix:

###---- SHOW THE WINDOW
self.Show(True)
self.SetMinSize(self.GetSize())

General comments:

The code is not bad. Just a few comments:

  • use object properties for the widgets, so you do not loose track of them (self.ChooseRoot =...)
  • use more desriptive widget names (self.labelChooseRoot)
  • drop IDs, when not needed, -1 is default value
  • drop redundant comments not giving any new information (self.CreateStatusBar() # Creates statusbar)
  • You can drop True value from calls self.Show(True) and self.Close(True), as True is default value
  • I would recommend using this kind of constructor for derived widget classes, but that comes from my personal preference:

.

def __init__(self, *args, **kwargs):
    wx.Frame.__init__(self, *args, **kwargs)


标签: wxpython