【问题标题】:Sizing a wx.TextCtrl widget?调整 wx.TextCtrl 小部件的大小?
【发布时间】:2013-11-09 20:58:06
【问题描述】:

我正在学习使用 wxWidgets 和 Python,但在确定如何在框架内调整小部件的大小时遇到​​了一些麻烦。

我的印象是,我可以通过在调用构造函数时给它们一个自定义 size=(x,y) 值来设置各种小部件的大小。这段代码是从 wxPython 的示例中复制并粘贴出来的,我已将 value="example",pos=(0,0) 和 size=(100,100) 值添加到 wx.TextCtrl() 构造函数中,但是当我运行时在这个程序中,文本控件占据了整个 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()

【问题讨论】:

    标签: python wxpython wxwidgets


    【解决方案1】:

    请阅读手册中的sizers overview 以了解如何正确调整小部件的大小。

    至于您的特定示例,这是一个例外,因为wxFrame 总是调整其唯一窗口的大小以填充其整个客户区 - 只是因为这是您几乎总是想要的。然而,通常这个唯一的窗口是一个wxPanel,它又包含其他控件并使用大小调整器来定位它们。

    TL;DR:您不应该使用绝对定位,即以像素为单位指定位置。

    【讨论】:

    • 我非常不同意你永远不应该使用绝对定位的评论
    • 即使在 2013 年(或 2003 年)使用绝对定位也是一个非常糟糕的主意,但由于高 DPI 屏幕的普及,在 2018 年它是一个可怕的主意。
    【解决方案2】:

    你肯定需要阅读这本书:wxPython 2.8 Application Development Cookbook

    阅读 -> 第 7 章:窗口布局和设计


    在您的代码中,添加小部件持有者:面板。

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 2014-02-17
      • 2016-09-10
      • 1970-01-01
      相关资源
      最近更新 更多