繁体   English   中英

wxPython:使用GridBagSizer

[英]wxPython: Using a GridBagSizer

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

本应用程序应该向我展示一个包含GridBagSize的框架,该框架应加载并GridBagSize 4个按钮。 但是,我遇到的一个小问题是我的4个按钮在屏幕的左上角很小。

为了澄清起见,我使用适用于Windows 32位的Python 2.7.8和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大小调整器,可以自动调整框架的大小。

显然,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)

首先,我要感谢您在理解GridBagSizer方面给我的帮助。

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

实际上,我使用了AddGrowableRowAddGrowableCol方法。

我在下面将您的全部代码重新贴上,希望对我这样的初学者有所帮助。

如果您在此代码中发现任何错误,请告诉我。

最好的祝福。

#!/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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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