开发者

Issue with wxPython mulltiline text box being too large for wx.Frame - can I auto "shrink" to the correct size of the text box?

Not sure if I am asking this correctly (e.g. using widget/object in the correct context). This code is meant just as an experiment (learning about classes and wxPython right now) - so it might not make sense as functional code.

Problem: I have a window/frame that I am allowing the user to specify width/height, and there is a multiline text box that is nested inside the window/frame. Originally, I was setting the dimensions of the multiline text box to the same size of the parent frame, but the scroll bar was being hidden because the scroll bar of the text box is larger than the frame/text box size. So... I decided to just manually adjust the size of the text box to make sure the size of the text box accounted for the scroll bar (see code below, specifically lines "sizexTxt = sizex - 17" and "sizeyTxt = sizey - 44").

Question: Is there some sort of way for adjusting the text box other than manually adjusting pixel by pixel? Is there a suggestion someone can offer that would be considered pythonic?

import wx

class myFrame(wx.Frame):
      def __init__(self, parent, title, sizexy, sizexyTxt):
          wx.Frame.__init__(self, parent, title=title, size=sizexy)
          self.Show(True)
          wx.TextCtrl(self, style=wx.TE_MULTILINE, size=(sizexyTxt))
          pass
开发者_如何学运维
sizex = input("X= ")
sizey = input("Y= ")
sizexy=(sizex, sizey)

sizexTxt = sizex - 17
sizeyTxt = sizey - 44
sizexyTxt = (sizexTxt, sizeyTxt)

myApp = wx.App(False)
frameNow = myFrame(None,"This is my title", sizexy, sizexyTxt)
myApp.MainLoop()


Jake pointed you in a right direction, but there is one more thing. You should be aware that first object which goes into the frame is wrapped into the basic sizer. The problem is that as far as I know you have no control of this sizer. So you have to add your own sizer, which you can use for frame fitting. You need the control over the sizer, because you would like the frame to change its size according to sizer.

import wx

class myFrame(wx.Frame):
    def __init__(self, size, *arg, **kwarg):
        wx.Frame.__init__(self, *arg, **kwarg)
        self.text = wx.TextCtrl(self, style=wx.TE_MULTILINE, size=size)

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.text, proportion=1, flag=wx.EXPAND)
        self.SetSizerAndFit(self.sizer)

        self.Show()


myApp = wx.App(False)

size = []
for a in ["X", "Y"]:
    d = wx.NumberEntryDialog(None, "Text size", "%s: " % a, "Window size entry", 100, 50, 200)
    if d.ShowModal() == wx.ID_OK:
        size.append(d.GetValue())

frameNow = myFrame(size, None)
myApp.MainLoop()


You'll want to use a sizer (or sizers) and then specify wx.EXPAND when adding to make it fit the sizer horizontally and wx.ALL to fit vertically.

Check out the sizer documentation here: http://wiki.wxpython.org/Getting%20Started#Sizers

Actually you might want to read the whole Working with Windows section, very useful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜