2

wxWidgets と Python の使い方を学んでいますが、フレーム内でウィジェットのサイズを調整する方法がわかりません。

コンストラクターを呼び出すときにカスタム size=(x,y) 値を与えることで、さまざまなウィジェットのサイズを設定できるという印象を受けています。このコードは wxPython の例からコピー アンド ペーストされ、wx.TextCtrl() コンストラクターに value="example",pos=(0,0) および size=(100,100) の値を追加しましたが、実行するとこのプログラムでは、テキスト コントロールが 500x500 フレーム全体を占めています。理由はわかりませんが、それを機能させるために何か助けていただければ幸いです。

import wx

class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(500,500))
        self.control = wx.TextCtrl(self,-1,value="example",pos=(0,0),size=(100,100))
        self.CreateStatusBar() # A Statusbar in the bottom of the window

        # Setting up the menu.
        filemenu= wx.Menu()

        # wx.ID_ABOUT and wx.ID_EXIT are standard IDs provided by wxWidgets.
        filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
        filemenu.AppendSeparator()
        filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")

        # Creating the menubar.
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
        self.Show(True)

app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()
4

2 に答える 2

2

ウィジェットのサイズを正しく設定する方法については、マニュアルのサイザーの概要をお読みください。

あなたの特定の例については、wxFrameクライアント領域全体を埋めるために常に唯一のウィンドウのサイズを変更するという事実による例外です-これはほとんど常に必要なものだからです。ただし、通常、この唯一のウィンドウは、wxPanel他のコントロールを含み、サイザーを使用してそれらを配置します。

TL;DR : 絶対配置、つまり位置をピクセル単位で指定する方法は絶対に使用しないでください。

于 2013-11-09T21:37:57.050 に答える
1

必ずこの本を読む必要があります: wxPython 2.8 Application Development Cookbook

読む ->第 7 章: ウィンドウのレイアウトとデザイン


コードに、widgets holder: panel を追加します。

import wx

class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(500,500))

        panel = wx.Panel(self)
        self.control = wx.TextCtrl(panel,-1,value="example",pos=(0,0),size=(100,100))
        self.CreateStatusBar() # A Statusbar in the bottom of the window

        # Setting up the menu.
        filemenu= wx.Menu()

        # wx.ID_ABOUT and wx.ID_EXIT are standard IDs provided by wxWidgets.
        filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
        filemenu.AppendSeparator()
        filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")

        # Creating the menubar.
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
        self.Show(True)

app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()

ここに画像の説明を入力

于 2013-11-10T19:14:28.597 に答える