2

私は wxpython GUI に取り組んでいます。私は 3 つのパネルを持っています (左側の 2 つは水平に分割され、右側の 1 つは他の 2 つから垂直に分割されます。左半分に 2 つの正方形があり、右半分に背の高い長方形があると考えてください)。パネル 3 (画像の P3) に Gridsizer を取り付けようとしています。グリッド パネルでパネル 3 全体を「埋める」ことができないようです。代わりに、グリッド パネルは上部と下部にスナップします。理想的には、10 個の小さなパネル (2 行、5 列) のサイズを変更して、パネル 3 をうまく塗りつぶします (すべて同じサイズで、それぞれを表示するのに十分な大きさです)。私は何を間違っていますか?ありがとう!

**** 編集 **** コードを次のように変更しました。

        sizer_31 = wx.BoxSizer(wx.VERTICAL)
        gs = wx.GridSizer(0,4,7,7)

        for i in self.Panel_Others.keys():
            gs.Add(self.Panel_Others[i],0,wx.ALIGN_CENTER|wx.ALL,5)

        sizer_31.Add(gs,0,wx.ALIGN_CENTER|wx.ALL,5)
        self.OtherTeams.SetSizer(sizer_31)
        sizer_31.SetSizeHints(self.OtherTeams)

私のNew Panel 3は下の写真のように見えます。これはある程度の改善です。ただし、個々のパネル (全部で 10 個) を同じ量だけ拡大して、ノートブック ページ全体が同じサイズの小さなパネルで覆われるようにしたいと考えています (カレンダーがどのように見え、毎日がパネルであるかを想像してください)。

ここに画像の説明を入力

****編集終了****

以下は私の元のコードです:

注: self.OtherTeamsは の下の wx.Notebook ページself.Panel3です。self.Panel_Others私が表示しているwx.Panelsを含む辞書です(これは動的に変化するため、指定するのではなく、それらの辞書を持っています)。

        sizer_31 = wx.BoxSizer(wx.HORIZONTAL)
        gs = wx.GridSizer(2,5,5,5)

        for i in self.Panel_Others.keys():
            sizer_temp = wx.BoxSizer(wx.VERTICAL)
            sizer_temp.Add(self.Panel_Others[i],1,wx.EXPAND)
            gs.Add(sizer_temp,1,wx.EXPAND,0)

        sizer_31.Add(gs,0,wx.EXPAND)
        self.OtherTeams.SetSizer(sizer_31)

ここに画像の説明を入力

4

1 に答える 1

1

これwxPython 2.8により、ページの左上隅に1つのぼかしが発生します。 wxPython 2.9サイザーに関しては少し賢いので、ある程度機能しているように見えます。

まず、GridSizer内を作成しますBoxSizerが、動的に拡張することはできません(wx.EXPAND)。次に、ページ(パネル)の代わりにサイザーを設定します。wx.Notebookこれにより、レイアウト全体が下に2.8移動し、グリッドサイザーのコントロール/パネルの一番下の行がトリミングされます。または、すべてが無視されて、すべてが左上隅。

これでうまくいくはずです:

sizer_31 = wx.BoxSizer(wx.VERTICAL) # page (panel) sizer for outer border
gs = wx.GridSizer(0,4,7,7) # content sizer

for i in self.Panel_Others.keys():
    gs.Add(self.Panel_Others[i],0,wx.EXPAND|wx.ALL,5) 
    # + wx.EXPAND: allow the content to fill the 'cells'
    #              (proportion is ignored here so it can be 0)
sizer_31.Add(gs,1,wx.EXPAND|wx.ALL,5) 
# + 1, wx.EXPAND: allow the grid to expand to the main sizer
#                 which in turn fits to the page
self.Panel3.SetSizer(sizer_31) # set the sizer to page, not the notebook

編集:次の例は、wx.Notebookページのwx.GridSizerのコンテンツを動的に更新する方法を示しています。

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='wx.Notebook')
        # create a wx.Notebook
        book = wx.Notebook(self)
        # create a new page; wx.Notebook owns the page
        # therefore the notebook must be the parent window
        page = MyPage(parent=book)
        # add the page to the notebook:
        book.AddPage(page, 'Page 1')
        book.AddPage(MyPage(book),'Page 2')
        book.AddPage(MyPage(book),'Page 3')

class MyPage(wx.Panel):
    "a page for wx.Notebook"
    def __init__(self, parent):
        # NOTE: wxPython 2.9.4/winXP may create graphic 
        # artifacts when more than one page is added. This can
        # be avoided by creating the page with a size of (0,0).
        # the reason for this is: the page (here a panel) is
        # created with the default size and position (upper
        # left corner). Once the layout is applied, the page
        # is moved to the proper coordinates. However, the
        # wx.Notebook is not redrawn so it will leave a black
        # spot where the page was moved away from. Alternatively,
        # the frame or notebook can be Refresh()ed after the
        # frame is shown.
        wx.Panel.__init__(self, parent, size=(0,0))
        # create sizer to add a border
        pageSizer = wx.BoxSizer(wx.VERTICAL)
        # create a grid sizer for the content
        self.grid = wx.GridSizer(0, 4, 5, 5)
        # add the grid sizer to the page sizer:
        # to allow the grid to fill the entire page,
        # set proportion>0 and add the wx.EXPAND flag.
        # the border of 10 is the spacing between the
        # page and the cells of the grid
        pageSizer.Add(self.grid, 1, wx.EXPAND|wx.ALL, 10)
        # set the main sizer:
        self.SetSizer(pageSizer)
        # add content to the grid
        self.GenerateContent()
        # DEMO: click any white space on the page to 
        # generate new content
        self.Bind(wx.EVT_LEFT_DOWN, self.GenerateContent)

    def GenerateContent(self, event=None):
        "dynamically create page content"
        # remove all items from the grid and destroy them
        # NOTE: if you don't actually create new controls here,
        # but use a list of controls created elsewhere (like in
        # in your code), do not delete the items! Use:
        # deleteWindows=False instead.
        self.grid.Clear(deleteWindows=True)
        # generate new grid content
        for i in range(10):
            # the contents are owned by the page,
            # therefore the page must be the parent window:
            # NOTE: size=(0,0) here is not required but
            # eliminates a briefly shown graphic artifact
            cell = wx.Panel(self, size=(0,0))
            cell.SetBackgroundColour(self.NewColor())
            # add the content (cells) to the grid:
            # specify the wx.EXPAND flag to fit the
            # content to the grid cell
            self.grid.Add(cell, flag=wx.EXPAND)
        # DEMO: hint text
        self.grid.Add(wx.StaticText(self, label=
            'Click empty cell to update content ->'),
                      flag=wx.EXPAND)
        # recalculate the layout to move the new contents
        # of the grid sizer into place
        self.Layout()

    col = 0 # DEMO: coloring
    def NewColor(self):
        self.col = (self.col + 3) % 241
        return wx.Colour(0, self.col * 10 % 255, self.col % 255)

app = wx.App(False)
MyFrame().Show()
app.MainLoop()
于 2012-12-21T06:06:18.077 に答える