在具有多个的ToggleButtons面板只有一个按钮改变颜色 - wxPython的 [英] Only one button in a panel with multiple togglebuttons changes color - wxPython
问题描述
我想设置我的选择的切换按钮的颜色,我已经创建了面板。问题是,在我所显示我的面板上众多的切换按钮,当我想改变每个人只有最后一个按钮的颜色变化的颜色。这里是我的code:
进口WX类帧(wx.Frame):高清__init __(个体经营):
wx.Frame .__的init __(自我,无)
self.panel = wx.Panel(个体经营,wx.ID_ANY) self.sizer = wx.BoxSizer(wx.VERTICAL)
self.flags_panel = wx.Panel(个体经营,wx.ID_ANY,风格= wx.SUNKEN_BORDER) self.sizer.Add(self.flags_panel)
self.SetSizer(self.sizer,wx.EXPAND | wx.ALL)
self.flags =标志(self.flags_panel,[8,12])
self.flags.Show()类标志(wx.Panel):
高清__init __(自我,面板,num_flags = []):#,行= 0,列= 0,半径= 0 hspace = 0产品,VSPACE = 0,x_start = 0,y_start = 0
wx.Panel .__的init __(自我,面板,-1,大小=(350700)) NUM_ROWS = num_flags [0]
NUM_COLUMNS = num_flags [1]
x_pos_start = 10
y_pos_start = 10 I = x_pos_start
J = y_pos_start
按钮= []
在范围(NUM_COLUMNS)我:
buttons.append('切换按钮')
self.ButtonValue =假
在按键按钮:
指数= 0
而指数= 15!
self.Button = wx.ToggleButton(个体经营,-1,大小=(10,10),POS =(I,J))
self.Bind(wx.EVT_TOGGLEBUTTON,self.OnFlagCreation,self.Button)
self.Button.Show()
I + = 15
指数+ = 1
J + = 15
我= 10 self.Show()高清OnFlagCreation(个体经营,事件):
如果没有self.ButtonValue:
self.Button.SetBackgroundColour('#fe1919')
self.ButtonValue = TRUE
其他:
self.Button.SetBackgroundColour('#14e807)
self.ButtonValue =假如果__name__ =='__main__':
应用= wx.App(假)
帧=帧()
frame.Show()
app.MainLoop()
您的问题很简单。最后一个按钮总是改变,因为它是定义的最后一个按钮:
self.Button = wx.ToggleButton(个体经营,-1,大小=(10,10),POS =(I,J))
通过为
每一次循环中,你重新分配 self.Button
属性不同的按钮。你想要做的是从事件对象提取按钮并更改其背景色。因此,改变你的功能如下:
高清OnFlagCreation(个体经营,事件):
BTN = event.GetEventObject()
如果没有self.ButtonValue:
btn.SetBackgroundColour('#fe1919')
self.ButtonValue = TRUE
其他:
btn.SetBackgroundColour('#14e807)
self.ButtonValue =假
另请参阅:
I want to set the color of a toggle button of my choice in the panel that I have created. The problem is that in the numerous toggle buttons that I have displayed on my panel when I want to change the color of each one only the color of the last button changes. Here's my code:
import wx
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None)
self.panel = wx.Panel(self,wx.ID_ANY)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.flags_panel = wx.Panel(self, wx.ID_ANY, style = wx.SUNKEN_BORDER)
self.sizer.Add(self.flags_panel)
self.SetSizer(self.sizer,wx.EXPAND | wx.ALL)
self.flags = Flags(self.flags_panel, [8,12])
self.flags.Show()
class Flags (wx.Panel):
def __init__(self,panel, num_flags = []):#,rows = 0,columns = 0,radius = 0, hspace = 0, vspace = 0,x_start = 0, y_start = 0
wx.Panel.__init__(self,panel,-1, size = (350,700))
num_rows = num_flags[0]
num_columns = num_flags[1]
x_pos_start = 10
y_pos_start = 10
i = x_pos_start
j = y_pos_start
buttons = []
for i in range (num_columns):
buttons.append('toggle button')
self.ButtonValue = False
for button in buttons:
index = 0
while index != 15:
self.Button = wx.ToggleButton(self,-1,size = (10,10), pos = (i,j))
self.Bind(wx.EVT_TOGGLEBUTTON,self.OnFlagCreation, self.Button)
self.Button.Show()
i += 15
index += 1
j += 15
i = 10
self.Show()
def OnFlagCreation(self,event):
if not self.ButtonValue:
self.Button.SetBackgroundColour('#fe1919')
self.ButtonValue = True
else:
self.Button.SetBackgroundColour('#14e807')
self.ButtonValue = False
if __name__ == '__main__':
app = wx.App(False)
frame = Frame()
frame.Show()
app.MainLoop()
Your problem is quite simple. The last button is always changed because it's the last button defined:
self.Button = wx.ToggleButton(self,-1,size = (10,10), pos = (i,j))
Each time through the for
loop, you reassign the self.Button
attribute to a different button. What you want to do is extract the button from your event object and change its background color. So change your function to look like this:
def OnFlagCreation(self,event):
btn = event.GetEventObject()
if not self.ButtonValue:
btn.SetBackgroundColour('#fe1919')
self.ButtonValue = True
else:
btn.SetBackgroundColour('#14e807')
self.ButtonValue = False
See also:
这篇关于在具有多个的ToggleButtons面板只有一个按钮改变颜色 - wxPython的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!