关闭

Python Qt GUI快速编程-第五章的exercise的python3+pytq5+Qt Dersinger实现

标签: pythonqtgui
324人阅读 评论(0) 收藏 举报
分类:

**本文是本人用python3+pytq5+Qt desinger实现Python Qt GUI快速编程-第五章的exercise,请大家参考。
改动的地方:
1,QStringList()用list()代替。
2,self.emit(SIGNAL(“acceptedList(QStringList)”), self.stringlist)在这里完全没必要。**


#/home/yrd/eric_workspace/StringListDlg/Ui_StringListDlg.py

# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(404, 244)
        Dialog.setSizeGripEnabled(True)
        self.widget = QtWidgets.QWidget(Dialog)
        self.widget.setGeometry(QtCore.QRect(10, 10, 381, 222))
        self.widget.setObjectName("widget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.listWidget = QtWidgets.QListWidget(self.widget)
        self.listWidget.setObjectName("listWidget")
        self.horizontalLayout.addWidget(self.listWidget)
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setContentsMargins(5, -1, 5, -1)
        self.verticalLayout.setObjectName("verticalLayout")
        self.addButton = QtWidgets.QPushButton(self.widget)
        self.addButton.setObjectName("addButton")
        self.verticalLayout.addWidget(self.addButton)
        self.editButton = QtWidgets.QPushButton(self.widget)
        self.editButton.setObjectName("editButton")
        self.verticalLayout.addWidget(self.editButton)
        self.removeButton = QtWidgets.QPushButton(self.widget)
        self.removeButton.setObjectName("removeButton")
        self.verticalLayout.addWidget(self.removeButton)
        self.upButton = QtWidgets.QPushButton(self.widget)
        self.upButton.setObjectName("upButton")
        self.verticalLayout.addWidget(self.upButton)
        self.downButton = QtWidgets.QPushButton(self.widget)
        self.downButton.setObjectName("downButton")
        self.verticalLayout.addWidget(self.downButton)
        self.sortButton = QtWidgets.QPushButton(self.widget)
        self.sortButton.setObjectName("sortButton")
        self.verticalLayout.addWidget(self.sortButton)
        self.closeButton = QtWidgets.QPushButton(self.widget)
        self.closeButton.setObjectName("closeButton")
        self.verticalLayout.addWidget(self.closeButton)
        self.verticalLayout.setStretch(6, 1)
        self.horizontalLayout.addLayout(self.verticalLayout)
        self.horizontalLayout.setStretch(0, 1)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "StringListDlg"))
        self.addButton.setText(_translate("Dialog", "&Add..."))
        self.editButton.setText(_translate("Dialog", "&Edit..."))
        self.removeButton.setText(_translate("Dialog", "&Remove..."))
        self.upButton.setText(_translate("Dialog", "&Up"))
        self.downButton.setText(_translate("Dialog", "&Down"))
        self.sortButton.setText(_translate("Dialog", "&Sort"))
        self.closeButton.setText(_translate("Dialog", "Close"))

#/home/yrd/eric_workspace/StringListDlg/StringListDlg.py

# -*- coding: utf-8 -*-

"""
Module implementing StringListDlg.
"""

from PyQt5.QtCore import pyqtSlot,pyqtSignal,Qt
from PyQt5.QtWidgets import QDialog,QApplication,QInputDialog,QMessageBox,QLineEdit

from Ui_StringListDlg import Ui_Dialog
import sys

MAC = True
try:
    from PyQt5.QtGui import qt_mac_set_native_menubar
except ImportError:
    MAC = False

class StringListDlg(QDialog, Ui_Dialog):
    """
    Class documentation goes here.
    """
    def __init__(self, name, stringlist=None,parent=None):
        """
        Constructor

        @param parent reference to the parent widget
        @type QWidget
        """
        super(StringListDlg, self).__init__(parent)
        self.setupUi(self)

        self.name = name
        if stringlist is not None:
            self.listWidget.addItems(stringlist)
            self.listWidget.setCurrentRow(0)

        self.addButton.clicked.connect(self.add)
        self.editButton.clicked.connect(self.edit)
        self.removeButton.clicked.connect(self.remove)
        self.upButton.clicked.connect(self.up)
        self.downButton.clicked.connect(self.down)
        self.sortButton.clicked.connect(self.listWidget.sortItems)        
        self.closeButton.clicked.connect(self.accept)
        self.setWindowTitle("Edit {0} List".format(self.name))

        if not MAC:
            self.addButton.setFocusPolicy(Qt.NoFocus)
            self.editButton.setFocusPolicy(Qt.NoFocus)
            self.removeButton.setFocusPolicy(Qt.NoFocus)
            self.upButton.setFocusPolicy(Qt.NoFocus)
            self.downButton.setFocusPolicy(Qt.NoFocus)
            self.sortButton.setFocusPolicy(Qt.NoFocus)
            self.closeButton.setFocusPolicy(Qt.NoFocus)       

    def add(self):
        row = self.listWidget.currentRow()
        title = "Add {0}".format(self.name)
        string, ok = QInputDialog.getText(self, title, title)
        if ok and string:
            self.listWidget.insertItem(row, string)

    def edit(self):
        row = self.listWidget.currentRow()
        item = self.listWidget.item(row)
        if item is not None:
            title = "Edit {0}".format(self.name)
            string, ok = QInputDialog.getText(self, title, title,
                    QLineEdit.Normal, item.text())
            if ok and string:
                item.setText(string)

    def remove(self):
        row = self.listWidget.currentRow()
        item = self.listWidget.item(row)
        if item is None:
            return
        reply = QMessageBox.question(self, "Remove {0}".format(
                self.name), "Remove {0} `{1}'?".format(
                self.name, str(item.text())),
                QMessageBox.Yes|QMessageBox.No)
        if reply == QMessageBox.Yes:
            item = self.listWidget.takeItem(row)
            del item


    def up(self):
        row = self.listWidget.currentRow()
        if row >= 1:
            item = self.listWidget.takeItem(row)
            self.listWidget.insertItem(row - 1, item)
            self.listWidget.setCurrentItem(item)


    def down(self):
        row = self.listWidget.currentRow()
        if row < self.listWidget.count() - 1:
            item = self.listWidget.takeItem(row)
            self.listWidget.insertItem(row + 1, item)
            self.listWidget.setCurrentItem(item)

    def reject(self):
        self.accept()


    def accept(self):
        self.stringlist = list()
        for row in range(self.listWidget.count()):
            self.stringlist.append(self.listWidget.item(row).text())

        QDialog.accept(self)    


if __name__ == "__main__":
    fruit = ["Banana", "Apple", "Elderberry", "Clementine", "Fig",
        "Guava", "Mango", "Honeydew Melon", "Date", "Watermelon",
        "Tangerine", "Ugli Fruit", "Juniperberry", "Kiwi", "Lemon",
        "Nectarine", "Plum", "Raspberry", "Strawberry", "Orange"]
    app = QApplication(sys.argv)
    form = StringListDlg("Fruit", fruit)
    form.exec_()
    print("\n".join([str(x) for x in form.stringlist]))

运行结果如下:
这里写图片描述

这里写图片描述

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23754次
    • 积分:715
    • 等级:
    • 排名:千里之外
    • 原创:50篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论