wxPython:使用GridBagSizer

时间:2014-11-11 20:38:46

标签: python

我是python开发的初学者,我正在尝试创建一个简单的应用程序。

这个应用程序应该向我显示一个包含GridBagSize的框架,该框架应该加载和定位4个按钮。但是,我遇到的一个小问题是我的4个按钮在屏幕的左上方非常小。

为了澄清,我使用的是Python 2.7.8 for Windows 32-bit和wxPython 2.8.12.1。

我在下面附上我的代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-
# import wx Module
import wx
# Creating a class derived from wx.Frame
class MyFrame(wx.Frame):
    def __init__(self, title):
        super(MyFrame, self).__init__(parent=None, id=wx.ID_ANY, title=title,style=wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)
        # Creating a GridBagSizer
        frameSizer=wx.GridBagSizer(vgap=5, hgap=5)
        # Creating buttons inside the frame and positioning them in the GridBagSizer
        Button1=wx.Button(parent=self, id=wx.ID_ANY, label="Button 1")
        frameSizer.Add(item=Button1, pos=(0, 0), span=(2, 1), flag=wx.ALIGN_CENTRE)
        Button2=wx.Button(parent=self, id=wx.ID_ANY, label="Button 2")
        frameSizer.Add(item=Button2, pos=(0, 1), span=(2, 1), flag=wx.ALIGN_CENTRE)
        Button3=wx.Button(parent=self, id=wx.ID_ANY, label="Button 3")
        Button4=wx.Button(parent=self, id=wx.ID_CLOSE, label="Button 4")
        self.SetSizer(frameSizer)
        #frameSizer.SetSizeHints(self)
        # We set our frame dimansions
        self.SetSize((400, 250))
        # Event of buttons ((Only the closing event for the Button4)
        Button4.Bind(wx.EVT_BUTTON, self.OnClose)
    # Creating the class method associated with the action of Button4
    def OnClose(self, evt):
        self.Destroy()
class MyApp(wx.App):
    """
    Specific class to the application created
    """
    def OnInit(self):
        frame=MyFrame("Example of a Small Program")
        # Display frame
        frame.Show(True)
        # We put the frame in main window
        self.SetTopWindow(frame)
        return True
    # Method of closing
    def OnExit(self):
        result=wx.MessageDialog(parent=None, message="Goodbye", caption="Exit", style=wx.OK)
        # Display dialog goodbye
        result.ShowModal()
# Program execution
app=MyApp(redirect=False)
app.MainLoop()

感谢您的快速回复。

是的,我忘了在frameSizer中添加我的按钮3和4。

我将插入按钮代码放在下面。

在显示屏上,我仍然有我的按钮在屏幕的左上方缩小。

关于将跨度减少到1行和1列的建议,我的结果相同。

我想我必须使用AddGrowableCol和AddGrowableRow方法,但我不知道要使用它们。

使用GridSizer sizer,帧会自动调整大小。

显然GridBagSizer并非如此?

感谢您的帮助。

最好的问候。

        # Creating buttons inside the frame and positioning them in the GridBagSizer
        Button1=wx.Button(parent=self, id=wx.ID_ANY, label="Button 1")
        frameSizer.Add(item=Button1, pos=(0, 0), span=(2, 1), flag=wx.ALIGN_CENTRE)
        Button2=wx.Button(parent=self, id=wx.ID_ANY, label="Button 2")
        frameSizer.Add(item=Button2, pos=(0, 1), span=(2, 1), flag=wx.ALIGN_CENTRE)
        Button3=wx.Button(parent=self, id=wx.ID_ANY, label="Button 3")
        frameSizer.Add(item=Button3, pos=(2, 0), span=(2, 1), flag=wx.ALIGN_CENTRE)
        Button4=wx.Button(parent=self, id=wx.ID_CLOSE, label="Button 4")
        frameSizer.Add(item=Button4, pos=(2, 1), span=(2, 1), flag=wx.ALIGN_CENTRE)
        self.SetSizer(frameSizer)

1 个答案:

答案 0 :(得分:0)

首先,我想感谢您对GridBagSizer的理解给予的帮助。

我终于找到了解决问题的方法。

事实上,我使用 AddGrowableRow AddGrowableCol 方法。

我在下面的代码中重申了你的所有代码,希望这对像我这样的初学者有所帮助。

如果您在此代码中发现任何错误,请与我们联系。

最好的问候。

#!/usr/bin/python
# -*- coding: utf-8 -*-
# import wx Module
import wx
# Creating a class derived from wx.Frame
class MyFrame(wx.Frame):
    def __init__(self, title):
        super(MyFrame, self).__init__(parent=None, id=wx.ID_ANY, title=title,style=wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)
        # Creating a GridBagSizer
        frameSizer=wx.GridBagSizer(vgap=5, hgap=5)
        # Creating buttons inside the frame and positioning them in the GridBagSizer
        Button1=wx.Button(parent=self, id=wx.ID_ANY, label="Button 1")
        frameSizer.Add(item=Button1, pos=(0, 0), span=(2, 1), flag=wx.ALIGN_CENTRE)
        Button2=wx.Button(parent=self, id=wx.ID_ANY, label="Button 2")
        frameSizer.Add(item=Button2, pos=(0, 1), span=(2, 1), flag=wx.ALIGN_CENTRE)
        Button3=wx.Button(parent=self, id=wx.ID_ANY, label="Button 3")
        frameSizer.Add(item=Button3, pos=(2, 0), span=(2, 1), flag=wx.ALIGN_CENTRE)
        Button4=wx.Button(parent=self, id=wx.ID_CLOSE, label="Button 4")
        frameSizer.Add(item=Button4, pos=(2, 1), span=(2, 1), flag=wx.ALIGN_CENTRE)
        self.SetSizer(frameSizer)
        frameSizer.SetSizeHints(self)
        # We resize our columns by the method AddGrowableCol
        for i in range(2):
            frameSizer.AddGrowableCol(i)
        # We resize our rows by the method AddGrowableRow
        for i in range(4):
            frameSizer.AddGrowableRow(i)
        # We set our frame dimansions
        self.SetSize((400, 250))
        # Event of buttons ((Only the closing event for the Button4)
        Button4.Bind(wx.EVT_BUTTON, self.OnClose)
    # Creating the class method associated with the action of Button4
    def OnClose(self, evt):
        self.Destroy()
class MyApp(wx.App):
    """
    Specific class to the application created
    """
    def OnInit(self):
        frame=MyFrame("Example of a Small Program")
        # Display frame
        frame.Show(True)
        # We put the frame in main window
        self.SetTopWindow(frame)
        return True
    # Method of closing
    def OnExit(self):
        result=wx.MessageDialog(parent=None, message="Goodbye", caption="Exit", style=wx.OK)
        # Display dialog goodbye
        result.ShowModal()
# Program execution
app=MyApp(redirect=False)
app.MainLoop()