WxPython - 在添加新内容时调整WxFrame的大小?

时间:2010-09-18 06:32:45

标签: python wxpython

几乎就像听起来一样。我有Wx.Frame中的按钮,这些按钮是动态创建的,我希望父框架在我添加新按钮时增加高度。已经从按钮总数乘以一个等于每个按钮高度的整数来获取高度,但我不知道如何在添加新按钮时根据该按钮更改大小。

作为一个侧面问题,我更新按钮的当前方法会产生令人讨厌的闪烁,我想知道是否有人有任何想法来解决这个问题。

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

2 个答案:

答案 0 :(得分:1)

除非您知道自己在做什么,否则

不要为您的方法添加前缀。这与您的问题没有直接关系,但它会导致您以后不会理解的错误。

请参阅此stackoverflow问题和the python documentation内容/原因。

答案 1 :(得分:1)

AFAIK无法自动调整框架大小,但您可以使用SetSize()手动重置框架尺寸

e.g。

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

为了获得理想的结果,尽管你需要使用sizer,但是我认为使用绝对定位并不是一个很好的理由。我还建议使用一个面板,它提供窗口小部件之间的选项卡遍历和跨平台布局的一致性。


Zetcode Sizer Tutorial

wxPython Sizer Tutorial