Michael Fryer
Michael Fryer

Reputation: 359

Sizing a wx.TextCtrl widget?

I'm in the process of learning to use wxWidgets and Python, but I'm having some trouble figuring out how to size widgets within a frame.

I am under the impression that I can set the size of various widgets by giving them a custom size=(x,y) value when calling the constructor. This code is copied and pasted out of the examples for wxPython and I have added the value="example",pos=(0,0) and size=(100,100) values to the wx.TextCtrl() constructor, but when I run this program, the text control takes up the entirety of the 500x500 frame. I'm not sure why, and I'd appreciate any help you could give me to get it to work.

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

Upvotes: 2

Views: 1205

Answers (2)

user1630938
user1630938

Reputation:

For sure you need to read from this book: wxPython 2.8 Application Development Cookbook

Read -> Chapter 7: Window Layout and Design


In your code, add 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()

enter image description here

Upvotes: 1

VZ.
VZ.

Reputation: 22753

Please read the sizers overview in the manual to know about how to size widgets correctly.

As for your particular example, it's an exception due to the fact that wxFrame always resizes its only window to fill its entire client area -- just because this is what you almost always want. However typically this only window is a wxPanel which, in turn, contains other controls and uses sizers to position them.

TL;DR: You should never use absolute positioning, i.e. specifying positions in pixels.

Upvotes: 2

Related Questions