Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the size of wx.TextCtrl dynamically

I have a problem with wx.TextCtrl. I just want to set the size of the control box but i want to do it dynamically. And this does not work for any reason.

Here i have my source code.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx

class Test(wx.Frame):

def __init__(self, parent):
    super().__init__(parent)

    # Panel
    self.panel = wx.Panel(self)

    # Boxes
    self.numbers = wx.StaticBox(self.panel, wx.ID_ANY, "Numbers")

    # Elements
    self.stN1 = wx.StaticText(self.numbers, wx.ID_ANY, "Number 1:")
    self.tcN1 = wx.TextCtrl(self.numbers, wx.ID_ANY, style=wx.TE_CENTER)
    self.tcN1.SetSize(wx.Size(36, 20))

    self.stN2 = wx.StaticText(self.numbers, wx.ID_ANY, "Number 2:")
    self.tcN2 = wx.TextCtrl(self.numbers, wx.ID_ANY, size=wx.Size(36, 20), style=wx.TE_CENTER)

    # Sizers  
    self.szTop = wx.BoxSizer(wx.VERTICAL)
    self.szNumbers = wx.StaticBoxSizer(self.numbers, wx.VERTICAL)
    self.szN1 = wx.BoxSizer(wx.HORIZONTAL)
    self.szN2 = wx.BoxSizer(wx.HORIZONTAL)

    self.szN1.Add(self.stN1, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
    self.szN1.Add(self.tcN1, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

    self.szN2.Add(self.stN2, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
    self.szN2.Add(self.tcN2, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

    self.szNumbers.Add(self.szN1)
    self.szNumbers.Add(self.szN2)

    self.szTop.Add(self.szNumbers, 0, wx.ALL, 5)

    self.panel.SetSizerAndFit(self.szTop)

    self.Fit()
    self.Show()


if __name__ == "__main__":
    app = wx.App()
    Test(None)
    app.MainLoop()

This code will give me the following result:

So as you see, the size was set properly in TextCtrl2, when I set it in the definition of the control. But I have lots of such controls, so I want to set the size dynamically in a method "SetSizeOfTextControl(textControl)". (Because if I decide to use another size later, I need to change only 1 line in this method and not 20 lines in all definitions.)

So I tried to use the given method wx.TextCtrl.SetSize() as you see in my TextCtrl1. But this does not work. Well, it does but only until I call SetSizersAndFit(). In this moment the size is set back to its default again.

What is the right way to do this? How to change the size of a wx.TextCtrl dynamically while using sizers? :)

Thanks a lot!

like image 277
Max16hr Avatar asked Oct 24 '25 19:10

Max16hr


2 Answers

If a widget's minimum size is set then the sizers will use that value when calculating the layout, and when you pass the size to the widget's constructor then it also sets the minimum size. But calling SetSize does not change the min size and so the next time the sizer does its Layout then it will reset the widget's size to the min size instead of the current size. In most cases like this you really want to call SetMinSize followed by calling the parent widget's Layout method.

Also, this can be very helpful for helping you to solve layout problems, or even just getting to understand better how things work: https://wiki.wxpython.org/Widget_Inspection_Tool

like image 153
RobinDunn Avatar answered Oct 27 '25 08:10

RobinDunn


Have you considered making size a variable?

size1 = wx.Size(36,20)

self.tcN1 = wx.TextCtrl(self.numbers, wx.ID_ANY, size=size1, style=wx.TE_CENTER)
self.tcN2 = wx.TextCtrl(self.numbers, wx.ID_ANY, size=size1, style=wx.TE_CENTER)
like image 31
Rolf of Saxony Avatar answered Oct 27 '25 08:10

Rolf of Saxony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!