0

我有一个空间,我希望某些元素(这里由 A、B、D 和 G 表示)每个都位于设计的自己的“角落”中。角应该对齐,就好像四个元素中的每一个都在排斥另一个一样;一个矩形。这将包含在不可调整大小的面板中。我将有几个类似的面板,并希望尽可能保持元素的位置相同。(我需要一些比 wx.Wizard 更复杂的东西,但总体思路相同。)

AAAAAAAAAA      BB


CCCCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCC
CCCCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCCCC


DDD EEE FFF    GGG

A 表示大字体的文本。

B 表示小字体的数字进度表(例如“1 of 7”)。

C 代表一大块文本。

D、E、F 和 G 是按钮。

G 按钮与其他按钮的功能分开。

我尝试过嵌套wx.BoxSizers(一个垂直框内的水平框),但没有运气。我的第一个问题wx.BoxSizer.SetMinSize我的最后一行没有得到尊重。第二个问题是我不知道如何让 G 按钮“占用空间”而不会变得可笑地变大,或者我不知道如何将它卡在右边缘和底部边缘。

我曾尝试使用 a wx.GridBagSizer,但遇到了完全不同的问题。

在浏览了各种在线教程和wxPython in Action之后,我有点沮丧。相关论坛似乎每两周就会出现一次活动。“玩弄它”让我无处可去;我觉得好像我正在努力抚平铺垫不当的地毯上的驼峰。

4

3 回答 3

4

使用 BoxSizers 和一些标志,如 wx.EXPAND 和 wx.ALIGN_RIGHT 等,你可以得到你想要的。这是一个例子

import wx
import string
AF = string.ascii_uppercase[:7]

class MyFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, size=(300,300))

        a, b, c, d, e, f, g = [wx.Button(self, -1, 4*c) for c in AF]
        a.SetMaxSize((200,100))   # 'a' won't get bigger than 200 wide

        # build the top row (with sizers I usually work from inside to out)
        hs0 = wx.BoxSizer(wx.HORIZONTAL)
        hs0.Add(a, 1, wx.ALIGN_LEFT)  # 1 here means expand with weight 1
        hs0.Add((0,0), 1)  # also expand the spacing
        hs0.Add(b, 0, wx.ALIGN_RIGHT)  # 0 means don't expand this

        # build the bottom row
        hs1 = wx.BoxSizer(wx.HORIZONTAL)
        hs1.Add(d, 0, wx.ALIGN_LEFT)
        hs1.Add((10,10), 0)
        hs1.Add(e, 0)
        hs1.Add((10,10), 0)
        hs1.Add(f, 0)
        hs1.Add((10, 10), 1)
        hs1.Add(g, 0, wx.ALIGN_RIGHT)

        # stack the rows vertically
        vs = wx.BoxSizer(wx.VERTICAL)
        vs.Add(hs0, flag=wx.EXPAND)   # expand in the orthogonal direction to sizer (here horizontal)
        vs.Add(c, 1, flag=wx.EXPAND)  
        vs.Add(hs1, flag=wx.EXPAND)
        self.SetSizer(vs)

app = wx.PySimpleApp()
frame = MyFrame()
frame.Show(1)
app.MainLoop()

替代文字

于 2010-03-31T01:08:32.807 回答
1

首先,您需要一个垂直大小调整器将窗口分成 3 行:AB、C 和 DEFG:

frame_sizer = wx.BoxSizer(wx.VERTICAL)

接下来,您需要第一行的水平大小调整器:

first_sizer = wx.BoxSizer(wx.HORIZONTAL)
# add the items A and B to first_sizer
# now we add first_sizer to frame_sizer
frame_sizer.Add(first_sizer)

然后,对于第二行,我们只需将 C 控件添加到 sizer:

frame_sizer.Add(C)

...确保以下几点:

  • 控件以指定的大小创建(通过其构造函数)
  • 控件具有最小尺寸 ( C.SetMinSize())

现在,对于最后一行,您最好的选择可能是使用另一个水平尺寸器。

wx.BoxSizer(wx.HORIZONTAL)
# add DEF
# now add some space between them
wx.AddSizer(10)
# now add G

希望这对你有用。如果没有,最好的办法是在 Code::Blocks 中使用像 wxSmith 这样的 GUI 设计器(它是为 C++ 设计的,但至少可以用来了解事物的外观 - wxPython 应该具有 1-to -1 对应于 wxWidgets)

于 2010-03-30T18:15:04.257 回答
0

我只有一帧。我在框架中内置了几个面板。它们都是隐藏的,只有一个。“下一步”按钮在该面板上执行 .Hide(),然后调用名为 .ShowYourself 的函数。我想要一些类似巫师的东西,你可以在上一个问题中看到

我正在尝试在面板上放置一个尺寸测定器。正如您从图片 http://tinypic.com/r/14nh651/5中看到的那样,我无法让 sizer 占据整个面板。图片未在预览中显示,因此位于http://tinypic.com/r/14nh651/5

import wx
import sys

class MyApp(
    wx.App      # This makes a subclass of the wx.App class

):

def OnInit(self):
    # image = wx.Image('wxPython.jpg', wx.BITMAP_TYPE_JPEG)
    self.frame = MyFrame(None)
    self.frame.Show()
    self.SetTopWindow(self.frame)
    return True

def OnExit(self):
    print 'Dying ...'

####################################################################################################

class MyFrame(
    wx.Frame    # This makes a subclass of the wx.Frame class
    ):
    """Frame class that displays an image."""

    def __init__(self, 
        image, 
        parent=None,                # "None" as a parameter means this is a top-level window
        id=-1,                  # "-1" means automatically generate a new ID
        title='Generic Title',          # What words appear in the title bar?
        pos=wx.DefaultPosition,         # Where is the upper left-hand corner?
        style=wx.CAPTION | wx.STAY_ON_TOP   # Style with only a caption
        ):      

    """Create a wx.Frame instance and display image."""

    size = (500, 500)
    wx.Frame.__init__(self, parent, id, 'Program Title', pos, size, style)

    sizer_h = wx.BoxSizer(wx.HORIZONTAL)

    self.panelX = TestLayout3(self)     
    sizer_h.Add(self.panelX)

    self.SetSizer(sizer_h)

    self.panelX.ShowYourself()

def ShutDown(self):

        self.Destroy()

####################################################################################################

class TestLayout3(wx.Panel):

# 0 - Splash screen with "Next" and "Cancel and Exit" buttons

def __init__(self, parent, id=-1):
    size = (600, 600)
    wx.Panel.__init__(self, parent, id, size)

    a, b, c, d, e, f, g = [wx.Button(self, -1, 4*c) for c in 'ABCDEFG']
    a.SetMaxSize((200,100))   # 'a' won't get bigger than 200 wide

    # build the top row (with sizers I usually work from inside to out)
    hs0 = wx.BoxSizer(wx.HORIZONTAL)
    hs0.Add(a, 1, wx.ALIGN_LEFT)  # 1 here means expand with weight 1
    hs0.Add((0,0), 1)  # also expand the spacing
    hs0.Add(b, 0, wx.ALIGN_RIGHT)  # 0 means don't expand this

    # build the bottom row
    hs2 = wx.BoxSizer(wx.HORIZONTAL)
    hs2.Add(d, 0, wx.ALIGN_LEFT)
    hs2.Add((10,10), 0)
    hs2.Add(e, 0)
    hs2.Add((10,10), 0)
    hs2.Add(f, 0)
    hs2.Add((10, 10), 1)
    hs2.Add(g, 0, wx.ALIGN_RIGHT)

    # stack the rows vertically
    vs = wx.BoxSizer(wx.VERTICAL)
    vs.Add(hs0, flag=wx.EXPAND)   # expand in the orthogonal direction to sizer (here horizontal)
    vs.Add(c, 1, flag=wx.EXPAND)  
    vs.Add(hs2, flag=wx.EXPAND)
    self.SetSizer(vs)

    self.Raise()
    self.SetPosition((0,0))
    self.Fit()  
    self.Hide()


# reveal this panel
def ShowYourself(self):
    self.Raise()
    self.SetPosition((0,0))
    self.Fit()
    self.Show()


# what do we do when the "Back" button is clicked?
def OnBack(self, event):
    self.Hide()
    self.GetParent().panel1.ShowYourself()

# what do we do when the "Next" button is clicked?
def OnNext(self, event):
    self.Hide()
    self.GetParent().panel1.ShowYourself()

    ![# what do we do when the "Cancel and Exit" button is clicked?
    def OnCancelAndExit(self, event):
        self.GetParent().ShutDown()

####################################################################################################

def main():
    app = MyApp(redirect = False)
    app.MainLoop()

####################################################################################################

if __name__ == '__main__':
    main()][3]
于 2010-03-31T15:57:50.603 回答