繁体   English   中英

调整wx.TextCtrl小部件的大小?

[英]Sizing a wx.TextCtrl widget?

我正在学习使用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()

请阅读手册中的sizer概述 ,以了解如何正确调整窗口小部件的大小。

对于您的特定示例,由于wxFrame始终会调整其唯一窗口的大小以填充其整个客户区域,因此这是一个例外-正是因为这几乎是您一直想要的。 但是,通常,唯一的窗口是wxPanel ,该窗口又包含其他控件并使用大小调整器定位它们。

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

当然,您需要阅读这本书: wxPython 2.8 Application Development Cookbook

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


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

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM