简体   繁体   中英

wxPython panel SetBackgroundColour on button event

I am trying to add a feature to a calculator i have made with wxpython, i want there to be a button , that when clicked changes the background colour (the panel). To show you my code i have made a smaller program, that should only change colour, and even in this one i get the same outcome: the background colour doesn't change, nothing happens when i click the button, and i dont even receive any errormessahe.Actually, the calculator does change colour, but not in the way i want it to, it only changes the colour of the text (a wx.StaticText), and it's not really meant to do that. Anyway, here is the code :

import wx

class calc(wx.Frame):


    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,"Calculator",size=(400,400))
        global panel
        panel=wx.Panel(self)
        a=wx.Button(panel,label="GO",pos=(100,100),size=(50,50))
        self.Bind(wx.EVT_BUTTON, self.change, a)
    def change(self,event):
        panel.SetBackgroundColour("red")






if __name__=="__main__":
    app=wx.App(False)
    frame=calc(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

when i run this, the frame with the button show up, and when i click on the button, nothing happens!! Does anybody know what is wrong with this? Thanks in advice!!!

While your code worked for me on Xubuntu 14.04, wxPython 2.8.12 and Python 2.7, I went ahead and rewrote it slightly to remove the global and clean it up a bit:

import wx

class calc(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title="Calculator", size=(400,400))
        self.panel = wx.Panel(self)

        a = wx.Button(self.panel, label="GO", pos=(100,100), size=(50,50))
        self.Bind(wx.EVT_BUTTON, self.change, a)

    def change(self,event):
        self.panel.SetBackgroundColour("red")
        self.Refresh()  # for windows

if __name__=="__main__":
    app = wx.App(False)
    frame = calc()
    frame.Show()
    app.MainLoop()

This also works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM