python 使用QSignalMapper制作动态复选框/按钮/ MenuItems / Everything
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用QSignalMapper制作动态复选框/按钮/ MenuItems / Everything相关的知识,希望对你有一定的参考价值。
from PyQt4 import QtGui
from PyQt4 import QtCore
import sys
class DynamicUI(QtGui.QDialog):
def __init__(self, *args, **kws):
parent = kws.get('parent', None)
self.dict_letter = kws.get('dict_letter', None)
super(DynamicUI, self).__init__(parent=parent)
## Class variables
self.current_letter_list = ''
self.save_list = []
self.save_dict = {}
self.mapper = None
## Setup GUI
self.create_window()
self.setup_connections()
self._letter_list.addItems(self.dict_letter.keys())
def create_window(self):
"""
Purpose: Contains all window elements
"""
## Font Variable
fontsize = QtGui.QFont()
## Window Title
self.setWindowTitle('Dynamic GUI Items')
## Main Layout
main_layout = QtGui.QVBoxLayout()
main_layout.addStretch(0)
self.setLayout(main_layout)
## SECOND ROW
## FIRST ROW
hori_Layout = QtGui.QGridLayout()
main_layout.addLayout(hori_Layout)
## File List -------------------------------->
groupbox_widget = QtGui.QGroupBox('File List')
groupbox_widget.setContentsMargins(4, 19, 2, 2)
groupbox_widget.setAlignment(4)
box = QtGui.QVBoxLayout()
groupbox_widget.setLayout(box)
hori_Layout.addWidget(groupbox_widget, 0, 0)
groupbox_widget.setFixedSize(250, 400)
# groupbox_widget.setMinimumWidth(300)
qlist = QtGui.QListWidget()
self._letter_list = qlist
fontsize = QtGui.QFont()
fontsize.setPointSize(10)
qlist.setFont(fontsize)
box.addWidget(qlist)
self._addFile_button = QtGui.QPushButton('Add Letter Manually')
self._removeFile_button = QtGui.QPushButton('Remove Selected')
box.addWidget(self._addFile_button)
box.addWidget(self._removeFile_button)
## Item List -------------------------------->
groupbox_widget = QtGui.QGroupBox('Items in List')
groupbox_widget.setContentsMargins(4, 19, 2, 2)
groupbox_widget.setAlignment(4)
box = QtGui.QVBoxLayout()
groupbox_widget.setLayout(box)
groupbox_widget.setFixedSize(300, 400)
self.number_layout = box
hori_Layout.addWidget(groupbox_widget, 0, 1)
## Translate ------------------>
self._translate_button = QtGui.QPushButton('Translate')
main_layout.addWidget(self._translate_button)
def setup_connections(self):
'''
Purpose: Connect all GUI Elemenents to functions
'''
self._letter_list.itemSelectionChanged.connect(self.file_list_clicked)
# self._addFile_button.clicked.connect(self.addFile_button_clicked)
self._removeFile_button.clicked.connect(self.removeFile_button_clicked)
def _update_button_clicked(self):
'''
Populate File List
'''
## Reset Table
self._letter_list.clear()
dataPath = str(self._data_path_line.text())
if not self.logic.checkPath(dataPath):
logReport.info(dataPath+' does not exist')
return
## Parse data info
result = self.logic.get_data(dataPath)
if result:
## Add Files to list
self._letter_list.addItems(self.logic.dataDict.keys())
def file_list_clicked(self):
'''
Populate Entity List with entity in file
'''
if len(self._letter_list.selectedItems()) == 0:
return
if self.mapper:
self.mapper.deleteLater()
self.save_list = []
self.mapper = QtCore.QSignalMapper(self)
## Grab selected file
self.letter = str(self._letter_list.selectedItems()[0].text())
number_list = self.dict_letter[self.letter]
if not number_list:
## Inform User
QtGui.QMessageBox.warning(self, 'Error Loading Numbers', 'Sorry, Letter {0} has no number list'.format(self.letter))
return
# Clear groupbox
[self.number_layout.takeAt(0).widget().deleteLater() for x in range(self.number_layout.count()-1)]
# Delete space
if self.number_layout.count():
self.number_layout.removeItem(self.number_layout.takeAt(0))
for each in number_list:
## Create Checkbox
checkbox = QtGui.QCheckBox()
checkbox.setChecked(True)
checkbox.setText(str(each))
self.number_layout.addWidget(checkbox)
## Connect check box to mapper
checkbox.clicked.connect(self.mapper.map)
## Connect check box to an id
self.mapper.setMapping(checkbox, str(each))
self.save_list.append(str(each))
self.mapper.mapped['QString'].connect(self.printThis)
# self.mapper.mapped['QString'].connect(lambda: self.number_toggle(str(checkbox.text()), checkbox))
space = QtGui.QSpacerItem(10, 10, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.number_layout.addItem(space)
def printThis(self, name):
print name
name = str(name)
if name in self.save_list:
self.save_list.remove(name)
else:
self.save_list.append(name)
# print self.save_list
self.save_dict[self.letter] = self.save_list
print self.save_dict
def removeFile_button_clicked(self):
'''
Remove path from table
'''
if len(self._letter_list.selectedIndexes()) == 0:
return
## Grab selected items and remove from list
for x in self._letter_list.selectedItems():
del self.logic.dataDict[str(x.text())]
## Add files back to list
# self._letter_list.addItems(self.logic.dataDict.keys())
def show_dialog():
"""
Shows the dialog as a singleton
"""
global DynamicUI_instance
try:
DynamicUI_instance.closeAndDeleteLater()
except:
pass
thisDict = {'a': [0, 1, 2], 'b': [3, 4, 5]}
DynamicUI_instance = DynamicUI(dict_letter=thisDict)
DynamicUI_instance.show()
# print DynamicUI_instance.size()
return DynamicUI_instance
def main():
app = QtGui.QApplication(sys.argv)
app.setStyle('plastique')
show_dialog()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
以上是关于python 使用QSignalMapper制作动态复选框/按钮/ MenuItems / Everything的主要内容,如果未能解决你的问题,请参考以下文章
让数据动起来!用Python制作动画可视化效果,让数据不再枯燥!
Qt:如何使用 QSignalMapper 为非标准按钮实现 QDialogButtonBox ?