• 317阅读
  • 0回复

解决第5章练习题‘’StringListDlg”对PyQt5+Python3兼容性的问题 [复制链接]

上一主题 下一主题
离线zyc921120
 

只看楼主 倒序阅读 楼主  发表于: 06-13
  1. #!/usr/bin/env python
  2. # Copyright (c) 2007-8 Qtrac Ltd. All rights reserved.
  3. # This program or module is free software: you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License as published
  5. # by the Free Software Foundation, either version 2 of the License, or
  6. # version 3 of the License, or (at your option) any later version. It is
  7. # provided for educational purposes and is distributed in the hope that
  8. # it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  9. # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  10. # the GNU General Public License for more details.
  11. import sys
  12. from PyQt5.QtCore import *
  13. from PyQt5.QtGui import *
  14. from PyQt5.QtWidgets import *
  15. MAC = "qt_mac_set_native_menubar" in dir()
  16. class StringListDlg(QDialog):
  17.     def __init__(self, name, stringlist=None, parent=None):
  18.         super(StringListDlg, self).__init__(parent)
  19.         self.name = name
  20.         self.listWidget = QListWidget()
  21.         if stringlist is not None:
  22.             self.listWidget.addItems(stringlist)
  23.             self.listWidget.setCurrentRow(0)
  24.         buttonLayout = QVBoxLayout()
  25.         for text, slot in (("&Add...", self.add),
  26.                            ("&Edit...", self.edit),
  27.                            ("&Remove...", self.remove),
  28.                            ("&Up", self.up),
  29.                            ("&Down", self.down),
  30.                            ("&Sort", self.listWidget.sortItems),
  31.                            ("Close", self.accept)):
  32.             button = QPushButton(text)
  33.             if not MAC:
  34.                 button.setFocusPolicy(Qt.NoFocus)
  35.             if text == "Close":
  36.                 buttonLayout.addStretch()
  37.             buttonLayout.addWidget(button)
  38.             button.clicked.connect(slot)
  39.         layout = QHBoxLayout()
  40.         layout.addWidget(self.listWidget)
  41.         layout.addLayout(buttonLayout)
  42.         self.setLayout(layout)
  43.         self.setWindowTitle("Edit %s List" % self.name)
  44.     def add(self):
  45.         row = self.listWidget.currentRow()
  46.         title = "Add %s" % self.name
  47.         string, ok = QInputDialog.getText(self, title, title)
  48.         if ok :
  49.             self.listWidget.insertItem(row, string)
  50.     def edit(self):
  51.         row = self.listWidget.currentRow()
  52.         item = self.listWidget.item(row)
  53.         if item is not None:
  54.             title = "Edit %s" % self.name
  55.             string, ok = QInputDialog.getText(self, title, title,
  56.                                 QLineEdit.Normal, item.text())
  57.             if ok :
  58.                 item.setText(string)
  59.     def remove(self):
  60.         row = self.listWidget.currentRow()
  61.         item = self.listWidget.item(row)
  62.         if item is None:
  63.             return
  64.         reply = QMessageBox.question(self, "Remove %s" % self.name,
  65.                         "Remove %s `%s'?" % (
  66.                         self.name, item.text()),
  67.                         QMessageBox.Yes|QMessageBox.No)
  68.         if reply == QMessageBox.Yes:
  69.             item = self.listWidget.takeItem(row)
  70.             del item
  71.     def up(self):
  72.         row = self.listWidget.currentRow()
  73.         if row >= 1:
  74.             item = self.listWidget.takeItem(row)
  75.             self.listWidget.insertItem(row - 1, item)
  76.             self.listWidget.setCurrentItem(item)
  77.     def down(self):
  78.         row = self.listWidget.currentRow()
  79.         if row < self.listWidget.count() - 1:
  80.             item = self.listWidget.takeItem(row)
  81.             self.listWidget.insertItem(row + 1, item)
  82.             self.listWidget.setCurrentItem(item)
  83.     def reject(self):
  84.         self.accept()
  85.     def accept(self):
  86.         self.stringlist = []
  87.         for row in range(self.listWidget.count()):
  88.             self.stringlist.append(self.listWidget.item(row).text())
  89.         
  90.         QDialog.accept(self)
  91.     
  92. if __name__ == "__main__":
  93.     fruit = ["Banana", "Apple", "Elderberry", "Clementine", "Fig",
  94.              "Guava", "Mango", "Honeydew Melon", "Date", "Watermelon",
  95.              "Tangerine", "Ugli Fruit", "Juniperberry", "Kiwi",
  96.              "Lemon", "Nectarine", "Plum", "Raspberry", "Strawberry",
  97.              "Orange"]
  98.     app = QApplication(sys.argv)
  99.     form = StringListDlg("Fruit", fruit)
  100.     form.exec_()
  101.     print("\n".join([str(x) for x in form.stringlist]))


快速回复
限100 字节
 
上一个 下一个