diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-04-23 09:07:15 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-04-23 12:30:22 +0200 |
commit | b7d4e231a95605ddcae75f32fdc33c473a5b5b90 (patch) | |
tree | 0c0556273aaff6ee6d2eea65747c6eb0987ef856 /examples/widgets/tutorials/addressbook/part6.py | |
parent | ff6a193f5478fe456a324926f038117d2ec74772 (diff) |
Remaining examples: Use per-class imports
Task-number: PYSIDE-1112
Change-Id: I8534e911959d6eed2ed6d3f7741e99929ff0125e
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'examples/widgets/tutorials/addressbook/part6.py')
-rw-r--r-- | examples/widgets/tutorials/addressbook/part6.py | 101 |
1 files changed, 52 insertions, 49 deletions
diff --git a/examples/widgets/tutorials/addressbook/part6.py b/examples/widgets/tutorials/addressbook/part6.py index f77bd2136..2bf694672 100644 --- a/examples/widgets/tutorials/addressbook/part6.py +++ b/examples/widgets/tutorials/addressbook/part6.py @@ -2,7 +2,7 @@ ############################################################################# ## ## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. +## Copyright (C) 2021 The Qt Company Ltd. ## Contact: http://www.qt.io/licensing/ ## ## This file is part of the Qt for Python examples of the Qt Toolkit. @@ -41,8 +41,13 @@ ############################################################################# import pickle +import sys -from PySide6 import QtCore, QtWidgets +from PySide6.QtCore import QFile, QIODevice, QTextStream, Qt, Signal, Slot +from PySide6.QtWidgets import (QApplication, QDialog, QFileDialog, + QGridLayout, QHBoxLayout, QLabel, QLineEdit, + QMessageBox, QPushButton, QTextEdit, + QVBoxLayout, QWidget) class SortedDict(dict): @@ -74,7 +79,7 @@ class SortedDict(dict): iterkeys = __iter__ -class AddressBook(QtWidgets.QWidget): +class AddressBook(QWidget): NavigationMode, AddingMode, EditingMode = range(3) def __init__(self, parent=None): @@ -85,34 +90,34 @@ class AddressBook(QtWidgets.QWidget): self._old_address = '' self._current_mode = self.NavigationMode - name_label = QtWidgets.QLabel("Name:") - self._name_line = QtWidgets.QLineEdit() + name_label = QLabel("Name:") + self._name_line = QLineEdit() self._name_line.setReadOnly(True) - address_label = QtWidgets.QLabel("Address:") - self._address_text = QtWidgets.QTextEdit() + address_label = QLabel("Address:") + self._address_text = QTextEdit() self._address_text.setReadOnly(True) - self._add_button = QtWidgets.QPushButton("&Add") - self._edit_button = QtWidgets.QPushButton("&Edit") + self._add_button = QPushButton("&Add") + self._edit_button = QPushButton("&Edit") self._edit_button.setEnabled(False) - self._remove_button = QtWidgets.QPushButton("&Remove") + self._remove_button = QPushButton("&Remove") self._remove_button.setEnabled(False) - self._find_button = QtWidgets.QPushButton("&Find") + self._find_button = QPushButton("&Find") self._find_button.setEnabled(False) - self._submit_button = QtWidgets.QPushButton("&Submit") + self._submit_button = QPushButton("&Submit") self._submit_button.hide() - self._cancel_button = QtWidgets.QPushButton("&Cancel") + self._cancel_button = QPushButton("&Cancel") self._cancel_button.hide() - self._next_button = QtWidgets.QPushButton("&Next") + self._next_button = QPushButton("&Next") self._next_button.setEnabled(False) - self._previous_button = QtWidgets.QPushButton("&Previous") + self._previous_button = QPushButton("&Previous") self._previous_button.setEnabled(False) - self._load_button = QtWidgets.QPushButton("&Load...") + self._load_button = QPushButton("&Load...") self._load_button.setToolTip("Load contacts from a file") - self._save_button = QtWidgets.QPushButton("Sa&ve...") + self._save_button = QPushButton("Sa&ve...") self._save_button.setToolTip("Save contacts to a file") self._save_button.setEnabled(False) @@ -129,7 +134,7 @@ class AddressBook(QtWidgets.QWidget): self._load_button.clicked.connect(self.load_from_file) self._save_button.clicked.connect(self.save_to_file) - button_layout_1 = QtWidgets.QVBoxLayout() + button_layout_1 = QVBoxLayout() button_layout_1.addWidget(self._add_button) button_layout_1.addWidget(self._edit_button) button_layout_1.addWidget(self._remove_button) @@ -140,14 +145,14 @@ class AddressBook(QtWidgets.QWidget): button_layout_1.addWidget(self._save_button) button_layout_1.addStretch() - button_layout_2 = QtWidgets.QHBoxLayout() + button_layout_2 = QHBoxLayout() button_layout_2.addWidget(self._previous_button) button_layout_2.addWidget(self._next_button) - main_layout = QtWidgets.QGridLayout() + main_layout = QGridLayout() main_layout.addWidget(name_label, 0, 0) main_layout.addWidget(self._name_line, 0, 1) - main_layout.addWidget(address_label, 1, 0, QtCore.Qt.AlignTop) + main_layout.addWidget(address_label, 1, 0, Qt.AlignTop) main_layout.addWidget(self._address_text, 1, 1) main_layout.addLayout(button_layout_1, 1, 2) main_layout.addLayout(button_layout_2, 2, 1) @@ -175,33 +180,33 @@ class AddressBook(QtWidgets.QWidget): address = self._address_text.toPlainText() if name == "" or address == "": - QtWidgets.QMessageBox.information(self, "Empty Field", + QMessageBox.information(self, "Empty Field", "Please enter a name and address.") return if self._current_mode == self.AddingMode: if name not in self.contacts: self.contacts[name] = address - QtWidgets.QMessageBox.information(self, "Add Successful", + QMessageBox.information(self, "Add Successful", f'"{name}" has been added to your address book.') else: - QtWidgets.QMessageBox.information(self, "Add Unsuccessful", + QMessageBox.information(self, "Add Unsuccessful", f'Sorry, "{name}" is already in your address book.') return elif self._current_mode == self.EditingMode: if self._old_name != name: if name not in self.contacts: - QtWidgets.QMessageBox.information(self, "Edit Successful", + QMessageBox.information(self, "Edit Successful", f'"{self.oldName}" has been edited in your address book.') del self.contacts[self._old_name] self.contacts[name] = address else: - QtWidgets.QMessageBox.information(self, "Edit Unsuccessful", + QMessageBox.information(self, "Edit Unsuccessful", f'Sorry, "{name}" is already in your address book.') return elif self._old_address != address: - QtWidgets.QMessageBox.information(self, "Edit Successful", + QMessageBox.information(self, "Edit Successful", f'"{name}" has been edited in your address book.') self.contacts[name] = address @@ -217,15 +222,15 @@ class AddressBook(QtWidgets.QWidget): address = self._address_text.toPlainText() if name in self.contacts: - button = QtWidgets.QMessageBox.question(self, "Confirm Remove", + button = QMessageBox.question(self, "Confirm Remove", f'Are you sure you want to remove "{name}"?', - QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) + QMessageBox.Yes | QMessageBox.No) - if button == QtWidgets.QMessageBox.Yes: + if button == QMessageBox.Yes: self.previous() del self.contacts[name] - QtWidgets.QMessageBox.information(self, "Remove Successful", + QMessageBox.information(self, "Remove Successful", f'"{name}" has been removed from your address book.') self.update_interface(self.NavigationMode) @@ -272,15 +277,15 @@ class AddressBook(QtWidgets.QWidget): def find_contact(self): self.dialog.show() - if self.dialog.exec_() == QtWidgets.QDialog.Accepted: + if self.dialog.exec_() == QDialog.Accepted: contact_name = self.dialog.get_find_text() if contact_name in self.contacts: self._name_line.setText(contact_name) self._address_text.setText(self.contacts[contact_name]) else: - QtWidgets.QMessageBox.information(self, "Contact Not Found", - f'Sorry, "{ccontact_nameontactName}" is not in your address book.') + QMessageBox.information(self, "Contact Not Found", + f'Sorry, "{contact_name}" is not in your address book.') return self.update_interface(self.NavigationMode) @@ -290,7 +295,7 @@ class AddressBook(QtWidgets.QWidget): if self._current_mode in (self.AddingMode, self.EditingMode): self._name_line.setReadOnly(False) - self._name_line.setFocus(QtCore.Qt.OtherFocusReason) + self._name_line.setFocus(Qt.OtherFocusReason) self._address_text.setReadOnly(False) self._add_button.setEnabled(False) @@ -329,7 +334,7 @@ class AddressBook(QtWidgets.QWidget): self._save_button.setEnabled(number >= 1) def save_to_file(self): - fileName,_ = QtWidgets.QFileDialog.getSaveFileName(self, + fileName,_ = QFileDialog.getSaveFileName(self, "Save Address Book", '', "Address Book (*.abk);;All Files (*)") @@ -339,7 +344,7 @@ class AddressBook(QtWidgets.QWidget): try: out_file = open(str(fileName), 'wb') except IOError: - QtWidgets.QMessageBox.information(self, "Unable to open file", + QMessageBox.information(self, "Unable to open file", f'There was an error opening "{fileName}"') return @@ -347,7 +352,7 @@ class AddressBook(QtWidgets.QWidget): out_file.close() def load_from_file(self): - fileName,_ = QtWidgets.QFileDialog.getOpenFileName(self, + fileName,_ = QFileDialog.getOpenFileName(self, "Open Address Book", '', "Address Book (*.abk);;All Files (*)") @@ -357,7 +362,7 @@ class AddressBook(QtWidgets.QWidget): try: in_file = open(str(fileName), 'rb') except IOError: - QtWidgets.QMessageBox.information(self, "Unable to open file", + QMessageBox.information(self, "Unable to open file", f'There was an error opening "{fileName}"') return @@ -365,7 +370,7 @@ class AddressBook(QtWidgets.QWidget): in_file.close() if len(self.contacts) == 0: - QtWidgets.QMessageBox.information(self, "No contacts in file", + QMessageBox.information(self, "No contacts in file", "The file you are attempting to open contains no " "contacts.") else: @@ -376,17 +381,17 @@ class AddressBook(QtWidgets.QWidget): self.update_interface(self.NavigationMode) -class FindDialog(QtWidgets.QDialog): +class FindDialog(QDialog): def __init__(self, parent=None): super(FindDialog, self).__init__(parent) - find_label = QtWidgets.QLabel("Enter the name of a contact:") - self._line_edit = QtWidgets.QLineEdit() + find_label = QLabel("Enter the name of a contact:") + self._line_edit = QLineEdit() - self._find_button = QtWidgets.QPushButton("&Find") + self._find_button = QPushButton("&Find") self._find_text = '' - layout = QtWidgets.QHBoxLayout() + layout = QHBoxLayout() layout.addWidget(find_label) layout.addWidget(self._line_edit) layout.addWidget(self._find_button) @@ -401,7 +406,7 @@ class FindDialog(QtWidgets.QDialog): text = self._line_edit.text() if not text: - QtWidgets.QMessageBox.information(self, "Empty Field", + QMessageBox.information(self, "Empty Field", "Please enter a name.") return @@ -414,9 +419,7 @@ class FindDialog(QtWidgets.QDialog): if __name__ == '__main__': - import sys - - app = QtWidgets.QApplication(sys.argv) + app = QApplication(sys.argv) address_book = AddressBook() address_book.show() |