开发者

WxPython - Resize WxFrame when adding new content?

Pretty much exactly as it sounds. I have buttons in a Wx.Frame that are created on the fly and I'd like the parent frame to increase in height as I add new buttons. The height is already being acquire from the total number of buttons multiplied by an integer equal the each button's height, but I don't know how to get the frame to change size based on that when new buttons are added.

As a side question the current method I have for updating the buttons creates a nasty flicker and I was wondering if anyone had any ideas for fixing that.

import wx
import mmap
import re

class pt:
    with open('note.txt', "r+") as note:
        buf = mmap.mmap(note.fileno(), 0)
        TL = 0
        readline = buf.readline
        while readline():
            TL += 1
        readlist = note.readlines()
    note.closed

class MainWindow(wx.Frame):

    def __init__(self, parent, title):
        w, h = wx.GetDisplaySize()
        self.x = w * 0
        self.y = h - bdepth

        self.container = wx.Frame.__init__(self, parent, title = title, pos = (self.x, self.y), size = (224, bdepth), style = wx.STAY_ON_TOP)


        self.__DoButtons()

        self.Show(True)

    def __DoButtons(self):

        for i, line in enumerate(pt.readlist):

            strip = line.rstrip('\n')
            todo = strip.lstrip('!')

            self.check = re开发者_如何学运维.match('!', strip)
            self.priority = re.search('(\!$)', strip)

            if self.check is None and self.priority is None:
                bullet = wx.Image('bullet.bmp', wx.BITMAP_TYPE_BMP)

                solid = wx.EmptyBitmap(200,64,-1)
                dc = wx.MemoryDC()
                dc.SelectObject(solid)
                solidpen = wx.Pen(wx.Colour(75,75,75),wx.SOLID)
                dc.SetPen(solidpen)
                dc.DrawRectangle(0, 0, 200, 64)
                dc.SetTextForeground(wx.Colour(255, 255, 255))
                dc.DrawBitmap(wx.BitmapFromImage(bullet, 32), 10, 28)
                dc.DrawText(todo, 30,  24)
                dc.SelectObject(wx.NullBitmap)

                hover = wx.EmptyBitmap(200,64,-1)
                dc = wx.MemoryDC()
                dc.SelectObject(hover)
                hoverpen = wx.Pen(wx.Colour(100,100,100),wx.SOLID)
                dc.SetPen(hoverpen)
                dc.DrawRectangle(0, 0, 200, 64)
                dc.SetTextForeground(wx.Colour(255, 255, 255))
                dc.DrawBitmap(wx.BitmapFromImage(bullet, 32), 10, 28)
                dc.DrawText(todo, 30,  24)
                dc.SelectObject(wx.NullBitmap)

                bmp = solid

            elif self.priority is None:
                checkmark = wx.Image('check.bmp', wx.BITMAP_TYPE_BMP)

                checked = wx.EmptyBitmap(200,64,-1)
                dc = wx.MemoryDC()
                dc.SelectObject(checked)
                checkedpen = wx.Pen(wx.Colour(50,50,50),wx.SOLID)
                dc.SetPen(checkedpen)
                dc.DrawRectangle(0, 0, 200, 50)
                dc.SetTextForeground(wx.Colour(200, 255, 0))
                dc.DrawBitmap(wx.BitmapFromImage(checkmark, 32), 6, 24)
                dc.DrawText(todo, 30,  24)
                dc.SelectObject(wx.NullBitmap)

                bmp = checked

            else:
                exclaim = wx.Image('exclaim.bmp', wx.BITMAP_TYPE_BMP)

                important = wx.EmptyBitmap(200,64,-1)
                dc = wx.MemoryDC()
                dc.SelectObject(important)
                importantpen = wx.Pen(wx.Colour(75,75,75),wx.SOLID)
                dc.SetPen(importantpen)
                dc.DrawRectangle(0, 0, 200, 50)
                dc.SetTextForeground(wx.Colour(255, 180, 0))
                dc.DrawBitmap(wx.BitmapFromImage(exclaim, 32), 6, 24)
                dc.DrawText(todo, 30,  24)
                dc.SelectObject(wx.NullBitmap)

                importanthover = wx.EmptyBitmap(200,64,-1)
                dc = wx.MemoryDC()
                dc.SelectObject(importanthover)
                importanthoverpen = wx.Pen(wx.Colour(100,100,100),wx.SOLID)
                dc.SetPen(importanthoverpen)
                dc.DrawRectangle(0, 0, 200, 50)
                dc.SetTextForeground(wx.Colour(255, 180, 0))
                dc.DrawBitmap(wx.BitmapFromImage(exclaim, 32), 6, 24)
                dc.DrawText(todo, 30,  24)
                dc.SelectObject(wx.NullBitmap)

                bmp = important

            b = wx.BitmapButton(self, i + 800, bmp, (10,  i * 64), (bmp.GetWidth(), bmp.GetHeight()), style = wx.NO_BORDER)


            if self.check is None and self.priority is None:
                b.SetBitmapHover(hover)
            elif self.priority is None:
                b.SetBitmapHover(checked)
            else:
                b.SetBitmapHover(importanthover)

        self.input = wx.TextCtrl(self, -1, "", (16, pt.TL * 64 + 4), (184, 24))
        self.Bind(wx.EVT_TEXT_ENTER, self.OnEnter, self.input)

    def OnClick(self, event):
        button = event.GetEventObject()
        button.None
        print('cheese')

    def OnEnter(self, event):
        value = self.input.GetValue()
        pt.readlist.append('\n' + value)

        self.__DoButtons()

        with open('note.txt', "r+") as note:
            for item in pt.readlist:
                note.write("%s" % item)
        note.closed

bdepth = pt.TL * 64 + 32
app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.SetTopWindow(frame)
app.MainLoop()


AFAIK there no way automatically resize the frame, but you can manually reset the size of your frame with SetSize()

e.g.

w, h = self.GetClientSize()
self.SetSize((w, h + height_of_your_new_button))

To the get the desired result though with minimum hassle you'll need to use sizers, I don't think theres ever a good reason to use absolute positioning. I would also recommend using a panel, which provides tab traversal between widgets and cross platform consistency of layout.


Zetcode Sizer Tutorial

wxPython Sizer Tutorial


Don't double-prefix your methods unless you know what you're doing. This is not directly related to your question, but it'll result in bugs you won't understand later.

See this stackoverflow question and the python documentation what/why.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜