Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Functioning example of how to dynamically generate buttons (or any element)
- # in PyQt4 and capture their emitted signals
- # Full code: www.github.com/mathomastech/HBS_Communicator
- # File of interest, handler.py
- import functools
- from PyQt4 import QtGui, QtCore
- from gui import GUI
- from communicator import Communicator
- class Handler(QtGui.QMainWindow):
- def __init__(self):
- super(Handler,self).__init__()
- self.generate_channels()
- def generate_channels(self):
- for i in range(0,len(Communicator.CHANNEL_LIST)):
- GUI.generate_channel_info(Communicator.CHANNEL_LIST[i][0],
- Communicator.CHANNEL_LIST[i][1],
- Communicator.CHANNEL_LIST[i][2],
- Communicator.CHANNEL_LIST[i][3]
- )
- for i in range(0,len(GUI.CHANNELS)):
- self.button = QtGui.QPushButton(GUI.CHANNELS[i][GUI.CHANNEL_NAME])
- self.button.clicked.connect(functools.partial(self.channel_clicked,GUI.CHANNELS[i][GUI.CHANNEL_NAME]))
- GUI.CHANNELS[i][GUI.GUI_ELEMENT] = self.button
- self.button.setFlat(True)
- self.button.setSizePolicy(sizePolicy)
- self.button.setAutoDefault(True)
- if GUI.CHANNELS[i][GUI.CHANNEL_GROUP] == "command":
- self.com.commandLayout.addWidget(self.button)
- elif GUI.CHANNELS[i][GUI.CHANNEL_GROUP] == "general":
- self.com.generalLayout.addWidget(self.button)
- def channel_clicked(self,channel,flag):
- Communicator.switch_channel(channel)
Advertisement
Add Comment
Please, Sign In to add comment