wxPython SetMinSize问题

发布于 2024-11-10 08:18:09 字数 3404 浏览 5 评论 0原文


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

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

我已经查看并搜索过,但没有任何帮助。
我也是编程新手,有人可以评论一下程序是如何构建的吗?是不是可以理解并且很好呢?或者应该采取什么措施?

感谢任何帮助。 =]

继承代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2024-11-17 08:18:09

修复:

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

一般评论:

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

  • 使用小部件的对象属性,这样您就不会失去对它们的跟踪(self.ChooseRoot =...
  • 使用更具描述性的小部件名称(self.labelChooseRoot) code>)
  • 删除 ID,不需要时,-1 是默认值
  • 删除不提供任何新信息的冗余注释 (self.CreateStatusBar() # Creates statusbar)
  • 您可以从调用中删除 True 值 self.Show(True)self.Close(True),因为 True 是默认值,
  • 我建议对派生小部件类使用这种构造函数,但这来自我个人的偏好:

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文