3

Edit: (little change -> 2nd problem) I created a BitmapButton & a TextCtrl. The picture in this button shall change when a certain text is entered in TextCtrl. This works:

def create(self,event):
    self.textinput = wx.TextCtrl(self.panel, pos=(100,20))
    self.picture = wx.Image("pics\\default.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
    self.picturebutton = wx.BitmapButton(self.panel,-1,self.picture,pos=(100,50))
    self.textinput.Bind(wx.EVT_CHAR, self.changepic)

def changepic(self,event):
    if self.textinput.GetValue = 'test':
        self.picturebutton.Destroy()
        self.picture = wx.Image("pics\\new.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        self.picturebutton = wx.BitmapButton(self.panel,-1,self.picture,pos=(100,50))
    event.Skip()

1.) I hope there is another way instead of destroying & rebuilding this button. I tried

self.picturebutton.Refresh()

and

self.picturebutton.Update()

instead of

    self.picturebutton.Destroy()
    self.picturebutton=wx.BitmapButton(self.panel,-1,self.picture,pos=(100,50))

but nothing happened. What can I do?

2.) It looks like "changepic" is called at first and then my TextCtrl receives the char. Because when I enter "test", nothing happens until i press another key. So the picture changes when I enter e.g. "testa". But it shall change when "test" is in the TextCtrl. How can I solve this? Is there a TextCtrl-Event which first puts the char in the TextCtrl and then call a function?

4

2 回答 2

0

是的,不需要重新创建控件来更改 bmp。刷新是您需要的,而不是在控件上调用它在控件父级上调用它。

self.Refresh()

使用 EVT_TEXT 事件时,您可以使用 event.String 来获取控件的内容

在这里您的代码与这些更改

def create(self,event):
    self.textinput = wx.TextCtrl(self.panel, pos=(100,20))
    self.picture = wx.Image("pics\\default.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
    self.picturebutton = wx.BitmapButton(self.panel,-1,self.picture,pos=(100,50))
    self.textinput.Bind(wx.EVT_TEXT, self.changepic)

def changepic(self,event):
    if event.String = 'test':
        self.picture = wx.Image("pics\\new.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        self.picturebutton.SetBitmap(self.picture)
        self.Refresh()
    event.Skip()
于 2013-03-31T23:18:57.937 回答
0

Yoriz 对 self.Refresh() 的使用很棒,但我不得不使用

self.picturebutton.SetBitmapLabel(self.picture)

而不是

self.picturebutton.SetBitmap(self.picture)

让它工作。SetBitmap似乎不是有效的 wx.BitmapButton 方法。(Python 2.7)

于 2013-12-20T21:32:38.800 回答