wxPython SetMinSize问题

时间:2011-05-31 10:09:51

标签: wxpython


在wxPython中设置SetMinSize时遇到问题。有人可以帮助我让这个窗口没有比启动时更小吗?和/或使窗口不允许调整大小?

我在MainPanel类中的self.SetMinSize(GetSize())之后尝试了self.SetSizerAndFit(SizerH)。没用。

我一直在寻找和搜索,但没有任何帮助 我也是编程新手,有人也可以评论程序是如何构建的吗?它可以理解和好吗?还是应该采取措施?

感谢任何帮助。 =]

下面是代码:

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 个答案:

答案 0 :(得分:2)

<强>修正:

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

一般评论:

代码还不错。只是一些评论:

  • 使用窗口小部件的对象属性,因此您不会忽略它们(self.ChooseRoot =...
  • 使用更具说服力的小部件名称(self.labelChooseRoot
  • 删除ID,不需要时,-1是默认值
  • 删除多余的评论,不提供任何新信息(self.CreateStatusBar() # Creates statusbar
  • 您可以从通话self.Show(True)self.Close(True)中删除True值,因为True是默认值
  • 我建议将这种构造函数用于派生的窗口小部件类,但这来自我个人的偏好:

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