wxPython:使用GridBagSiz

2024-09-29 23:26:44 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

为了澄清一下,我使用的是python2.7.8forwindows32位和wxpython2.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列,我有同样的结果。在

我想我必须使用addgrowtablecol和addgrowtablerow方法,但我不知道如何使用它们。在

使用GridSizer sizer,框架会自动调整大小。在

显然,这不是GridBagSizer的情况吗?在

谢谢你的帮助。在

谨致问候。在

^{pr2}$

Tags: theselfcreatingiddefanybutton按钮
1条回答
网友
1楼 · 发布于 2024-09-29 23:26:44

首先,我要感谢你在理解GridBagSizer的过程中给予我的帮助。在

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

事实上,我使用了addgrowtablerowaddGrowtableCol方法。在

我把我的全部代码重新写在这里,希望这能帮助像我这样的初学者。在

如果你在这段代码中发现任何错误,请告诉我。在

谨致问候。在

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

相关问题 更多 >

    热门问题