diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-03-22 15:41:48 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-03-22 19:41:03 +0000 |
commit | 570cc14c50782ed46393a0a6a8d9d491fbb3785a (patch) | |
tree | 9b123ff862fb83b2c70db4791792919ecc59bd47 /examples | |
parent | 80aec29aca246f1f67a4f8c453b49f1eadfee6fd (diff) |
Replace % formatting in examples by f-strings
As drive-by, Fix fortune server, addressbook and dombookmarks examples to work.
Task-number: PYSIDE-1112
Change-Id: I8ef7759ed56aeb7157cf2222bee9b6481973112a
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'examples')
30 files changed, 124 insertions, 104 deletions
diff --git a/examples/network/blockingfortuneclient/blockingfortuneclient.py b/examples/network/blockingfortuneclient/blockingfortuneclient.py index 4ee0ce622..536110d4e 100644 --- a/examples/network/blockingfortuneclient/blockingfortuneclient.py +++ b/examples/network/blockingfortuneclient/blockingfortuneclient.py @@ -205,7 +205,7 @@ class BlockingClient(QWidget): "and port settings are correct.") else: QMessageBox.information(self, "Blocking Fortune Client", - "The following error occurred: %s." % message) + f"The following error occurred: {message}.") self.getFortuneButton.setEnabled(True) diff --git a/examples/network/fortuneclient/fortuneclient.py b/examples/network/fortuneclient/fortuneclient.py index 99ca41b0e..2e6d6d559 100644 --- a/examples/network/fortuneclient/fortuneclient.py +++ b/examples/network/fortuneclient/fortuneclient.py @@ -118,7 +118,6 @@ class Client(QtWidgets.QDialog): return nextFortune = instr.readString() - nextFortune = str(nextFortune, encoding='ascii') if nextFortune == self.currentFortune: QtCore.QTimer.singleShot(0, self.requestNewFortune) @@ -141,8 +140,9 @@ class Client(QtWidgets.QDialog): "fortune server is running, and check that the host name " "and port settings are correct.") else: + reason = self.tcpSocket.errorString() QtWidgets.QMessageBox.information(self, "Fortune Client", - "The following error occurred: %s." % self.tcpSocket.errorString()) + f"The following error occurred: {reason}.") self.getFortuneButton.setEnabled(True) diff --git a/examples/network/fortuneserver/fortuneserver.py b/examples/network/fortuneserver/fortuneserver.py index 7b8f05a32..e3087f1c1 100644 --- a/examples/network/fortuneserver/fortuneserver.py +++ b/examples/network/fortuneserver/fortuneserver.py @@ -52,18 +52,20 @@ class Server(QtWidgets.QDialog): super(Server, self).__init__(parent) statusLabel = QtWidgets.QLabel() + statusLabel.setTextInteractionFlags(QtCore.Qt.TextBrowserInteraction) quitButton = QtWidgets.QPushButton("Quit") quitButton.setAutoDefault(False) self.tcpServer = QtNetwork.QTcpServer(self) if not self.tcpServer.listen(): + reason = self.tcpServer.errorString() QtWidgets.QMessageBox.critical(self, "Fortune Server", - "Unable to start the server: %s." % self.tcpServer.errorString()) + f"Unable to start the server: {reason}.") self.close() return - - statusLabel.setText("The server is running on port %d.\nRun the " - "Fortune Client example now." % self.tcpServer.serverPort()) + port = self.tcpServer.serverPort() + statusLabel.setText(f"The server is running on port {port}.\nRun the " + "Fortune Client example now.") self.fortunes = ( "You've been leading a dog's life. Stay off the furniture.", diff --git a/examples/network/threadedfortuneserver/threadedfortuneserver.py b/examples/network/threadedfortuneserver/threadedfortuneserver.py index 839a2a57e..deb993b71 100644 --- a/examples/network/threadedfortuneserver/threadedfortuneserver.py +++ b/examples/network/threadedfortuneserver/threadedfortuneserver.py @@ -111,8 +111,9 @@ class Dialog(QDialog): quitButton.setAutoDefault(False) if not self.server.listen(): + reason = self.server.errorString() QMessageBox.critical(self, "Threaded Fortune Server", - "Unable to start the server: %s." % self.server.errorString()) + f"Unable to start the server: {reason}.") self.close() return @@ -123,9 +124,10 @@ class Dialog(QDialog): ipAddress = QHostAddress(QHostAddress.LocalHost) ipAddress = ipAddress.toString() + port = self.server.serverPort() - statusLabel.setText("The server is running on\n\nIP: %s\nport: %d\n\n" - "Run the Fortune Client example now." % (ipAddress, self.server.serverPort())) + statusLabel.setText(f"The server is running on\n\nIP: {ipAddress}\nport: {port}\n\n" + "Run the Fortune Client example now.") quitButton.clicked.connect(self.close) diff --git a/examples/widgets/dialogs/classwizard/classwizard.py b/examples/widgets/dialogs/classwizard/classwizard.py index addc089c6..b8ad04514 100644 --- a/examples/widgets/dialogs/classwizard/classwizard.py +++ b/examples/widgets/dialogs/classwizard/classwizard.py @@ -122,8 +122,10 @@ class ClassWizard(QtWidgets.QWizard): headerFile = QtCore.QFile(outputDir + '/' + header) if not headerFile.open(QtCore.QFile.WriteOnly | QtCore.QFile.Text): + name = headerFile.fileName() + reason = headerFile.errorString() QtWidgets.QMessageBox.warning(None, "Class Wizard", - "Cannot write file %s:\n%s" % (headerFile.fileName(), headerFile.errorString())) + f"Cannot write file {name}:\n{reason}") return headerFile.write(QtCore.QByteArray(block.encode("utf-8"))) @@ -175,8 +177,10 @@ class ClassWizard(QtWidgets.QWizard): implementationFile = QtCore.QFile(outputDir + '/' + implementation) if not implementationFile.open(QtCore.QFile.WriteOnly | QtCore.QFile.Text): + name = implementationFile.fileName() + reason = implementationFile.errorString() QtWidgets.QMessageBox.warning(None, "Class Wizard", - "Cannot write file %s:\n%s" % (implementationFile.fileName(), implementationFile.errorString())) + f"Cannot write file {name}:\n{reason}") return implementationFile.write(QtCore.QByteArray(block.encode("utf-8"))) @@ -389,7 +393,7 @@ class ConclusionPage(QtWidgets.QWizardPage): def initializePage(self): finishText = self.wizard().buttonText(QtWidgets.QWizard.FinishButton) finishText.replace('&', '') - self.label.setText("Click %s to generate the class skeleton." % finishText) + self.label.setText(f"Click {finishText} to generate the class skeleton.") if __name__ == '__main__': diff --git a/examples/widgets/dialogs/findfiles/findfiles.py b/examples/widgets/dialogs/findfiles/findfiles.py index 5e26f73ad..50432e7ed 100644 --- a/examples/widgets/dialogs/findfiles/findfiles.py +++ b/examples/widgets/dialogs/findfiles/findfiles.py @@ -130,7 +130,8 @@ class Window(QtWidgets.QDialog): for i in range(len(files)): progressDialog.setValue(i) - progressDialog.setLabelText("Searching file number %d of %d..." % (i, len(files))) + n = len(files) + progressDialog.setLabelText(f"Searching file number {i} of {n}...") QtCore.QCoreApplication.processEvents() if progressDialog.wasCanceled(): @@ -159,7 +160,8 @@ class Window(QtWidgets.QDialog): fileNameItem = QtWidgets.QTableWidgetItem(fn) fileNameItem.setFlags(fileNameItem.flags() ^ QtCore.Qt.ItemIsEditable) - sizeItem = QtWidgets.QTableWidgetItem("%d KB" % (int((size + 1023) / 1024))) + size_kb = int((size + 1023) / 1024) + sizeItem = QtWidgets.QTableWidgetItem(f"{size_kb} KB") sizeItem.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight) sizeItem.setFlags(sizeItem.flags() ^ QtCore.Qt.ItemIsEditable) @@ -168,7 +170,8 @@ class Window(QtWidgets.QDialog): self.filesTable.setItem(row, 0, fileNameItem) self.filesTable.setItem(row, 1, sizeItem) - self.filesFoundLabel.setText("%d file(s) found (Double click on a file to open it)" % len(files)) + n = len(files) + self.filesFoundLabel.setText(f"{n} file(s) found (Double click on a file to open it)") def createButton(self, text, member): button = QtWidgets.QPushButton(text) diff --git a/examples/widgets/dialogs/standarddialogs/standarddialogs.py b/examples/widgets/dialogs/standarddialogs/standarddialogs.py index 5e0f80c4d..493f2bb54 100644 --- a/examples/widgets/dialogs/standarddialogs/standarddialogs.py +++ b/examples/widgets/dialogs/standarddialogs/standarddialogs.py @@ -185,7 +185,7 @@ class Dialog(QtWidgets.QDialog): i, ok = QtWidgets.QInputDialog.getInt(self, "QInputDialog.getInteger()", "Percentage:", 25, 0, 100, 1) if ok: - self.integerLabel.setText("%d%%" % i) + self.integerLabel.setText(f"{i}%") def setDouble(self): d, ok = QtWidgets.QInputDialog.getDouble(self, "QInputDialog.getDouble()", @@ -249,7 +249,8 @@ class Dialog(QtWidgets.QDialog): "All Files (*);;Text Files (*.txt)", "", options) if files: self.openFilesPath = files[0] - self.openFileNamesLabel.setText("[%s]" % ', '.join(files)) + file_list = ', '.join(files) + self.openFileNamesLabel.setText(f"[{file_list}]") def setSaveFileName(self): options = QtWidgets.QFileDialog.Options() diff --git a/examples/widgets/draganddrop/draggabletext/draggabletext.py b/examples/widgets/draganddrop/draggabletext/draggabletext.py index a9ef72c41..16e02cb90 100644 --- a/examples/widgets/draganddrop/draggabletext/draggabletext.py +++ b/examples/widgets/draganddrop/draggabletext/draggabletext.py @@ -62,8 +62,9 @@ class DragLabel(QLabel): mimeData = QMimeData() mimeData.setText(self.text()) - mimeData.setData('application/x-hotspot', - b'%d %d' % (hotSpot.x(), hotSpot.y())) + hx = hotSpot.x() + hy = hotSpot.y() + mimeData.setData('application/x-hotspot', f'{hx} {hy}'.encode('utf-8')) pixmap = QPixmap(self.size()) self.render(pixmap) diff --git a/examples/widgets/graphicsview/dragdroprobot/dragdroprobot.py b/examples/widgets/graphicsview/dragdroprobot/dragdroprobot.py index a72c9351b..95134c313 100644 --- a/examples/widgets/graphicsview/dragdroprobot/dragdroprobot.py +++ b/examples/widgets/graphicsview/dragdroprobot/dragdroprobot.py @@ -57,10 +57,9 @@ class ColorItem(QtWidgets.QGraphicsItem): self.color = QtGui.QColor(random(256), random(256), random(256)) + (r, g, b) = (self.color.red(), self.color.green(), self.color.blue()) self.setToolTip( - "QColor(%d, %d, %d)\nClick and drag this color onto the robot!" % - (self.color.red(), self.color.green(), self.color.blue()) - ) + f"QColor({r}, {g}, {b})\nClick and drag this color onto the robot!") self.setCursor(QtCore.Qt.OpenHandCursor) def boundingRect(self): diff --git a/examples/widgets/itemviews/addressbook/addresswidget.py b/examples/widgets/itemviews/addressbook/addresswidget.py index b3cc6591d..dfb0e42e5 100644 --- a/examples/widgets/itemviews/addressbook/addresswidget.py +++ b/examples/widgets/itemviews/addressbook/addresswidget.py @@ -94,7 +94,7 @@ class AddressWidget(QTabWidget): try: addresses.remove(address) QMessageBox.information(self, "Duplicate Name", - "The name \"%s\" already exists." % name) + f'The name "{name}" already exists.') except ValueError: # The address didn't already exist, so let's add it to the model. @@ -129,13 +129,14 @@ class AddressWidget(QTabWidget): # Get the name and address of the currently selected row. indexes = selectionModel.selectedRows() + if len(indexes) != 1: + return - for index in indexes: - row = proxyModel.mapToSource(index).row() - ix = self.tableModel.index(row, 0, QModelIndex()) - name = self.tableModel.data(ix, Qt.DisplayRole) - ix = self.tableModel.index(row, 1, QModelIndex()) - address = self.tableModel.data(ix, Qt.DisplayRole) + row = proxyModel.mapToSource(indexes[0]).row() + ix = self.tableModel.index(row, 0, QModelIndex()) + name = self.tableModel.data(ix, Qt.DisplayRole) + ix = self.tableModel.index(row, 1, QModelIndex()) + address = self.tableModel.data(ix, Qt.DisplayRole) # Open an addDialogWidget, and only allow the user to edit the address. addDialog = AddDialogWidget() @@ -216,12 +217,12 @@ class AddressWidget(QTabWidget): f = open(filename, "rb") addresses = pickle.load(f) except IOError: - QMessageBox.information(self, "Unable to open file: %s" % filename) + QMessageBox.information(self, f"Unable to open file: {filename}") finally: f.close() if len(addresses) == 0: - QMessageBox.information(self, "No contacts in file: %s" % filename) + QMessageBox.information(self, f"No contacts in file: {filename}") else: for address in addresses: self.addEntry(address["name"], address["address"]) @@ -233,7 +234,7 @@ class AddressWidget(QTabWidget): pickle.dump(self.tableModel.addresses, f) except IOError: - QMessageBox.information(self, "Unable to open file: %s" % filename) + QMessageBox.information(self, f"Unable to open file: {filename}") finally: f.close() diff --git a/examples/widgets/itemviews/fetchmore/fetchmore.py b/examples/widgets/itemviews/fetchmore/fetchmore.py index c91874004..281cc166b 100644 --- a/examples/widgets/itemviews/fetchmore/fetchmore.py +++ b/examples/widgets/itemviews/fetchmore/fetchmore.py @@ -132,7 +132,7 @@ class Window(QtWidgets.QWidget): self.setWindowTitle("Fetch More Example") def updateLog(self, number): - self.logViewer.append("%d items added." % number) + self.logViewer.append(f"{number} items added.") if __name__ == '__main__': diff --git a/examples/widgets/layouts/basiclayouts/basiclayouts.py b/examples/widgets/layouts/basiclayouts/basiclayouts.py index 25a9b7760..ea46ad1e9 100644 --- a/examples/widgets/layouts/basiclayouts/basiclayouts.py +++ b/examples/widgets/layouts/basiclayouts/basiclayouts.py @@ -91,7 +91,7 @@ class Dialog(QtWidgets.QDialog): layout = QtWidgets.QHBoxLayout() for i in range(Dialog.NumButtons): - button = QtWidgets.QPushButton("Button %d" % (i + 1)) + button = QtWidgets.QPushButton(f"Button {i + 1}") layout.addWidget(button) self.horizontalGroupBox.setLayout(layout) @@ -101,7 +101,7 @@ class Dialog(QtWidgets.QDialog): layout = QtWidgets.QGridLayout() for i in range(Dialog.NumGridRows): - label = QtWidgets.QLabel("Line %d:" % (i + 1)) + label = QtWidgets.QLabel(f"Line {i + 1}:") lineEdit = QtWidgets.QLineEdit() layout.addWidget(label, i + 1, 0) layout.addWidget(lineEdit, i + 1, 1) diff --git a/examples/widgets/mainwindows/application/application.py b/examples/widgets/mainwindows/application/application.py index 4fddff5c1..93500efe1 100644 --- a/examples/widgets/mainwindows/application/application.py +++ b/examples/widgets/mainwindows/application/application.py @@ -216,8 +216,9 @@ class MainWindow(QtWidgets.QMainWindow): def loadFile(self, fileName): file = QtCore.QFile(fileName) if not file.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text): + reason = file.errorString() QtWidgets.QMessageBox.warning(self, "Application", - "Cannot read file %s:\n%s." % (fileName, file.errorString())) + f"Cannot read file {fileName}:\n{reason}.") return inf = QtCore.QTextStream(file) @@ -236,9 +237,11 @@ class MainWindow(QtWidgets.QMainWindow): outf = QtCore.QTextStream(file) outf << self.textEdit.toPlainText() if not file.commit(): - error = "Cannot write file %s:\n%s." % (fileName, file.errorString()) + reason = file.errorString() + error = f"Cannot write file {fileName}:\n{reason}." else: - error = "Cannot open file %s:\n%s." % (fileName, file.errorString()) + reason = file.errorString() + error = f"Cannot open file {fileName}:\n{reason}." QtWidgets.QApplication.restoreOverrideCursor() if error: @@ -259,7 +262,7 @@ class MainWindow(QtWidgets.QMainWindow): else: shownName = 'untitled.txt' - self.setWindowTitle("%s[*] - Application" % shownName) + self.setWindowTitle(f"{shownName}[*] - Application") def strippedName(self, fullFileName): return QtCore.QFileInfo(fullFileName).fileName() diff --git a/examples/widgets/mainwindows/dockwidgets/dockwidgets.py b/examples/widgets/mainwindows/dockwidgets/dockwidgets.py index 910f0aa73..53428b82c 100644 --- a/examples/widgets/mainwindows/dockwidgets/dockwidgets.py +++ b/examples/widgets/mainwindows/dockwidgets/dockwidgets.py @@ -134,8 +134,9 @@ class MainWindow(QMainWindow): file = QFile(filename) if not file.open(QFile.WriteOnly | QFile.Text): + reason = file.errorString() QMessageBox.warning(self, "Dock Widgets", - "Cannot write file %s:\n%s." % (filename, file.errorString())) + "Cannot write file {filename}:\n{reason}.") return out = QTextStream(file) @@ -143,7 +144,7 @@ class MainWindow(QMainWindow): out << self.textEdit.toHtml() QApplication.restoreOverrideCursor() - self.statusBar().showMessage("Saved '%s'" % filename, 2000) + self.statusBar().showMessage(f"Saved '{filename}'", 2000) def undo(self): document = self.textEdit.document() diff --git a/examples/widgets/painting/basicdrawing/basicdrawing.py b/examples/widgets/painting/basicdrawing/basicdrawing.py index 9134ae49e..07e90ae22 100644 --- a/examples/widgets/painting/basicdrawing/basicdrawing.py +++ b/examples/widgets/painting/basicdrawing/basicdrawing.py @@ -153,8 +153,9 @@ class RenderArea(QWidget): elif self.shape == RenderArea.Path: painter.drawPath(path) elif self.shape == RenderArea.Text: + qv = qVersion() painter.drawText(rect, Qt.AlignCenter, - "PySide 2\nQt %s" % qVersion()) + f"PySide 6\nQt {qv}") elif self.shape == RenderArea.Pixmap: painter.drawPixmap(10, 10, self.pixmap) diff --git a/examples/widgets/richtext/orderform/orderform.py b/examples/widgets/richtext/orderform/orderform.py index ed7b29d61..3135e82e6 100644 --- a/examples/widgets/richtext/orderform/orderform.py +++ b/examples/widgets/richtext/orderform/orderform.py @@ -109,8 +109,8 @@ class MainWindow(QtWidgets.QMainWindow): cursor.insertBlock() date = QtCore.QDate.currentDate() - cursor.insertText("Date: %s" % date.toString('d MMMM yyyy'), - textFormat) + date_str = date.toString('d MMMM yyyy') + cursor.insertText(f"Date: {date_str}", textFormat) cursor.insertBlock() bodyFrameFormat = QtGui.QTextFrameFormat() diff --git a/examples/widgets/tutorials/addressbook/part2.py b/examples/widgets/tutorials/addressbook/part2.py index 92254ce1c..68446d3d0 100644 --- a/examples/widgets/tutorials/addressbook/part2.py +++ b/examples/widgets/tutorials/addressbook/part2.py @@ -141,10 +141,10 @@ class AddressBook(QtWidgets.QWidget): if name not in self.contacts: self.contacts[name] = address QtWidgets.QMessageBox.information(self, "Add Successful", - "\"%s\" has been added to your address book." % name) + f'"{name}" has been added to your address book.') else: QtWidgets.QMessageBox.information(self, "Add Unsuccessful", - "Sorry, \"%s\" is already in your address book." % name) + f'Sorry, "{name}" is already in your address book.') return if not self.contacts: diff --git a/examples/widgets/tutorials/addressbook/part3.py b/examples/widgets/tutorials/addressbook/part3.py index c04b8f2b9..cfb150d7f 100644 --- a/examples/widgets/tutorials/addressbook/part3.py +++ b/examples/widgets/tutorials/addressbook/part3.py @@ -154,10 +154,10 @@ class AddressBook(QtWidgets.QWidget): if name not in self.contacts: self.contacts[name] = address QtWidgets.QMessageBox.information(self, "Add Successful", - "\"%s\" has been added to your address book." % name) + f'"{name}" has been added to your address book.') else: QtWidgets.QMessageBox.information(self, "Add Unsuccessful", - "Sorry, \"%s\" is already in your address book." % name) + f'Sorry, "{name}" is already in your address book.') return if not self.contacts: diff --git a/examples/widgets/tutorials/addressbook/part4.py b/examples/widgets/tutorials/addressbook/part4.py index eca0be852..dbca3c099 100644 --- a/examples/widgets/tutorials/addressbook/part4.py +++ b/examples/widgets/tutorials/addressbook/part4.py @@ -165,26 +165,26 @@ class AddressBook(QtWidgets.QWidget): if name not in self.contacts: self.contacts[name] = address QtWidgets.QMessageBox.information(self, "Add Successful", - "\"%s\" has been added to your address book." % name) + f'"{name}" has been added to your address book.') else: QtWidgets.QMessageBox.information(self, "Add Unsuccessful", - "Sorry, \"%s\" is already in your address book." % name) + f'Sorry, "{name}" is already in your address book.') return elif self.currentMode == self.EditingMode: if self.oldName != name: if name not in self.contacts: QtWidgets.QMessageBox.information(self, "Edit Successful", - "\"%s\" has been edited in your address book." % self.oldName) + f'"{self.oldName}" has been edited in your address book.') del self.contacts[self.oldName] self.contacts[name] = address else: QtWidgets.QMessageBox.information(self, "Edit Unsuccessful", - "Sorry, \"%s\" is already in your address book." % name) + f'Sorry, "{name}" is already in your address book.') return elif self.oldAddress != address: QtWidgets.QMessageBox.information(self, "Edit Successful", - "\"%s\" has been edited in your address book." % name) + f'"{name}" has been edited in your address book.') self.contacts[name] = address self.updateInterface(self.NavigationMode) @@ -200,7 +200,7 @@ class AddressBook(QtWidgets.QWidget): if name in self.contacts: button = QtWidgets.QMessageBox.question(self, "Confirm Remove", - "Are you sure you want to remove \"%s\"?" % name, + f'Are you sure you want to remove "{name}"?', QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) if button == QtWidgets.QMessageBox.Yes: @@ -208,7 +208,7 @@ class AddressBook(QtWidgets.QWidget): del self.contacts[name] QtWidgets.QMessageBox.information(self, "Remove Successful", - "\"%s\" has been removed from your address book." % name) + f'"{name}" has been removed from your address book.') self.updateInterface(self.NavigationMode) diff --git a/examples/widgets/tutorials/addressbook/part5.py b/examples/widgets/tutorials/addressbook/part5.py index 27eb50a82..a513889ec 100644 --- a/examples/widgets/tutorials/addressbook/part5.py +++ b/examples/widgets/tutorials/addressbook/part5.py @@ -171,26 +171,26 @@ class AddressBook(QtWidgets.QWidget): if name not in self.contacts: self.contacts[name] = address QtWidgets.QMessageBox.information(self, "Add Successful", - "\"%s\" has been added to your address book." % name) + f'"{name}" has been added to your address book.') else: QtWidgets.QMessageBox.information(self, "Add Unsuccessful", - "Sorry, \"%s\" is already in your address book." % name) + f'Sorry, "{name}" is already in your address book.') return elif self.currentMode == self.EditingMode: if self.oldName != name: if name not in self.contacts: QtWidgets.QMessageBox.information(self, "Edit Successful", - "\"%s\" has been edited in your address book." % self.oldName) + f'"{self.oldName}" has been edited in your address book.') del self.contacts[self.oldName] self.contacts[name] = address else: QtWidgets.QMessageBox.information(self, "Edit Unsuccessful", - "Sorry, \"%s\" is already in your address book." % name) + f'Sorry, "{name}" is already in your address book.') return elif self.oldAddress != address: QtWidgets.QMessageBox.information(self, "Edit Successful", - "\"%s\" has been edited in your address book." % name) + f'"{name}" has been edited in your address book.') self.contacts[name] = address self.updateInterface(self.NavigationMode) @@ -206,7 +206,7 @@ class AddressBook(QtWidgets.QWidget): if name in self.contacts: button = QtWidgets.QMessageBox.question(self, "Confirm Remove", - "Are you sure you want to remove \"%s\"?" % name, + f'Are you sure you want to remove "{name}"?', QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) if button == QtWidgets.QMessageBox.Yes: @@ -214,7 +214,7 @@ class AddressBook(QtWidgets.QWidget): del self.contacts[name] QtWidgets.QMessageBox.information(self, "Remove Successful", - "\"%s\" has been removed from your address book." % name) + f'"{name}" has been removed from your address book.') self.updateInterface(self.NavigationMode) @@ -268,7 +268,7 @@ class AddressBook(QtWidgets.QWidget): self.addressText.setText(self.contacts[contactName]) else: QtWidgets.QMessageBox.information(self, "Contact Not Found", - "Sorry, \"%s\" is not in your address book." % contactName) + f'Sorry, "{contactName}" is not in your address book.') return self.updateInterface(self.NavigationMode) diff --git a/examples/widgets/tutorials/addressbook/part6.py b/examples/widgets/tutorials/addressbook/part6.py index 70103380c..370c1ba9c 100644 --- a/examples/widgets/tutorials/addressbook/part6.py +++ b/examples/widgets/tutorials/addressbook/part6.py @@ -183,26 +183,26 @@ class AddressBook(QtWidgets.QWidget): if name not in self.contacts: self.contacts[name] = address QtWidgets.QMessageBox.information(self, "Add Successful", - "\"%s\" has been added to your address book." % name) + f'"{name}" has been added to your address book.') else: QtWidgets.QMessageBox.information(self, "Add Unsuccessful", - "Sorry, \"%s\" is already in your address book." % name) + f'Sorry, "{name}" is already in your address book.') return elif self.currentMode == self.EditingMode: if self.oldName != name: if name not in self.contacts: QtWidgets.QMessageBox.information(self, "Edit Successful", - "\"%s\" has been edited in your address book." % self.oldName) + f'"{self.oldName}" has been edited in your address book.') del self.contacts[self.oldName] self.contacts[name] = address else: QtWidgets.QMessageBox.information(self, "Edit Unsuccessful", - "Sorry, \"%s\" is already in your address book." % name) + f'Sorry, "{name}" is already in your address book.') return elif self.oldAddress != address: QtWidgets.QMessageBox.information(self, "Edit Successful", - "\"%s\" has been edited in your address book." % name) + f'"{name}" has been edited in your address book.') self.contacts[name] = address self.updateInterface(self.NavigationMode) @@ -218,7 +218,7 @@ class AddressBook(QtWidgets.QWidget): if name in self.contacts: button = QtWidgets.QMessageBox.question(self, "Confirm Remove", - "Are you sure you want to remove \"%s\"?" % name, + f'Are you sure you want to remove "{name}"?', QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) if button == QtWidgets.QMessageBox.Yes: @@ -226,7 +226,7 @@ class AddressBook(QtWidgets.QWidget): del self.contacts[name] QtWidgets.QMessageBox.information(self, "Remove Successful", - "\"%s\" has been removed from your address book." % name) + f'"{name}" has been removed from your address book.') self.updateInterface(self.NavigationMode) @@ -280,7 +280,7 @@ class AddressBook(QtWidgets.QWidget): self.addressText.setText(self.contacts[contactName]) else: QtWidgets.QMessageBox.information(self, "Contact Not Found", - "Sorry, \"%s\" is not in your address book." % contactName) + f'Sorry, "{contactName}" is not in your address book.') return self.updateInterface(self.NavigationMode) @@ -340,7 +340,7 @@ class AddressBook(QtWidgets.QWidget): out_file = open(str(fileName), 'wb') except IOError: QtWidgets.QMessageBox.information(self, "Unable to open file", - "There was an error opening \"%s\"" % fileName) + f'There was an error opening "{fileName}"') return pickle.dump(self.contacts, out_file) @@ -358,7 +358,7 @@ class AddressBook(QtWidgets.QWidget): in_file = open(str(fileName), 'rb') except IOError: QtWidgets.QMessageBox.information(self, "Unable to open file", - "There was an error opening \"%s\"" % fileName) + f'There was an error opening "{fileName}"') return self.contacts = pickle.load(in_file) diff --git a/examples/widgets/tutorials/addressbook/part7.py b/examples/widgets/tutorials/addressbook/part7.py index c6a272636..d2c70d41a 100644 --- a/examples/widgets/tutorials/addressbook/part7.py +++ b/examples/widgets/tutorials/addressbook/part7.py @@ -189,26 +189,26 @@ class AddressBook(QtWidgets.QWidget): if name not in self.contacts: self.contacts[name] = address QtWidgets.QMessageBox.information(self, "Add Successful", - "\"%s\" has been added to your address book." % name) + f'"{name}" has been added to your address book.') else: QtWidgets.QMessageBox.information(self, "Add Unsuccessful", - "Sorry, \"%s\" is already in your address book." % name) + f'Sorry, "{name}" is already in your address book.') return elif self.currentMode == self.EditingMode: if self.oldName != name: if name not in self.contacts: QtWidgets.QMessageBox.information(self, "Edit Successful", - "\"%s\" has been edited in your address book." % self.oldName) + f'"{self.oldName}" has been edited in your address book.') del self.contacts[self.oldName] self.contacts[name] = address else: QtWidgets.QMessageBox.information(self, "Edit Unsuccessful", - "Sorry, \"%s\" is already in your address book." % name) + f'Sorry, "{name}" is already in your address book.') return elif self.oldAddress != address: QtWidgets.QMessageBox.information(self, "Edit Successful", - "\"%s\" has been edited in your address book." % name) + f'"{name}" has been edited in your address book.') self.contacts[name] = address self.updateInterface(self.NavigationMode) @@ -224,7 +224,7 @@ class AddressBook(QtWidgets.QWidget): if name in self.contacts: button = QtWidgets.QMessageBox.question(self, "Confirm Remove", - "Are you sure you want to remove \"%s\"?" % name, + f'Are you sure you want to remove "{name}"?', QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) if button == QtWidgets.QMessageBox.Yes: @@ -232,7 +232,7 @@ class AddressBook(QtWidgets.QWidget): del self.contacts[name] QtWidgets.QMessageBox.information(self, "Remove Successful", - "\"%s\" has been removed from your address book." % name) + f'"{name}" has been removed from your address book.') self.updateInterface(self.NavigationMode) @@ -286,7 +286,7 @@ class AddressBook(QtWidgets.QWidget): self.addressText.setText(self.contacts[contactName]) else: QtWidgets.QMessageBox.information(self, "Contact Not Found", - "Sorry, \"%s\" is not in your address book." % contactName) + f'Sorry, "{contactName}" is not in your address book.') return self.updateInterface(self.NavigationMode) @@ -349,7 +349,7 @@ class AddressBook(QtWidgets.QWidget): out_file = open(str(fileName), 'wb') except IOError: QtWidgets.QMessageBox.information(self, "Unable to open file", - "There was an error opening \"%s\"" % fileName) + f'There was an error opening "{fileName}"') return pickle.dump(self.contacts, out_file) @@ -367,7 +367,7 @@ class AddressBook(QtWidgets.QWidget): in_file = open(str(fileName), 'rb') except IOError: QtWidgets.QMessageBox.information(self, "Unable to open file", - "There was an error opening \"%s\"" % fileName) + f'There was an error opening "{fileName}"') return self.contacts = pickle.load(in_file) @@ -425,7 +425,7 @@ class AddressBook(QtWidgets.QWidget): out_s << 'END:VCARD' << '\n' QtWidgets.QMessageBox.information(self, "Export Successful", - "\"%s\" has been exported as a vCard." % name) + f'"{name}" has been exported as a vCard.') class FindDialog(QtWidgets.QDialog): diff --git a/examples/widgets/tutorials/cannon/t10.py b/examples/widgets/tutorials/cannon/t10.py index ca7e9d9b7..1cfed53aa 100644 --- a/examples/widgets/tutorials/cannon/t10.py +++ b/examples/widgets/tutorials/cannon/t10.py @@ -77,9 +77,9 @@ class LCDRange(QtWidgets.QWidget): def setRange(self, minValue, maxValue): if minValue < 0 or maxValue > 99 or minValue > maxValue: - QtCore.qWarning("LCDRange::setRange(%d, %d)\n" + QtCore.qWarning(f"LCDRange::setRange({minValue}, {maxValue})\n" "\tRange must be 0..99\n" - "\tand minValue must not be greater than maxValue" % (minValue, maxValue)) + "\tand minValue must not be greater than maxValue") return self.slider.setRange(minValue, maxValue) diff --git a/examples/widgets/tutorials/cannon/t11.py b/examples/widgets/tutorials/cannon/t11.py index c3d2e1271..dfc206d51 100644 --- a/examples/widgets/tutorials/cannon/t11.py +++ b/examples/widgets/tutorials/cannon/t11.py @@ -78,9 +78,9 @@ class LCDRange(QtWidgets.QWidget): def setRange(self, minValue, maxValue): if minValue < 0 or maxValue > 99 or minValue > maxValue: - QtCore.qWarning("LCDRange::setRange(%d, %d)\n" + QtCore.qWarning(f"LCDRange::setRange({minValue}, {maxValue})\n" "\tRange must be 0..99\n" - "\tand minValue must not be greater than maxValue" % (minValue, maxValue)) + "\tand minValue must not be greater than maxValue") return self.slider.setRange(minValue, maxValue) diff --git a/examples/widgets/tutorials/cannon/t12.py b/examples/widgets/tutorials/cannon/t12.py index 290ecc82e..b94a28688 100644 --- a/examples/widgets/tutorials/cannon/t12.py +++ b/examples/widgets/tutorials/cannon/t12.py @@ -95,9 +95,9 @@ class LCDRange(QtWidgets.QWidget): def setRange(self, minValue, maxValue): if minValue < 0 or maxValue > 99 or minValue > maxValue: - QtCore.qWarning("LCDRange::setRange(%d, %d)\n" + QtCore.qWarning(f"LCDRange::setRange({minValue}, {maxValue})\n" "\tRange must be 0..99\n" - "\tand minValue must not be greater than maxValue" % (minValue, maxValue)) + "\tand minValue must not be greater than maxValue") return self.slider.setRange(minValue, maxValue) diff --git a/examples/widgets/tutorials/cannon/t13.py b/examples/widgets/tutorials/cannon/t13.py index b54945e25..467346c2c 100644 --- a/examples/widgets/tutorials/cannon/t13.py +++ b/examples/widgets/tutorials/cannon/t13.py @@ -96,9 +96,9 @@ class LCDRange(QtWidgets.QWidget): def setRange(self, minValue, maxValue): if minValue < 0 or maxValue > 99 or minValue > maxValue: - QtCore.qWarning("LCDRange::setRange(%d, %d)\n" + QtCore.qWarning(f"LCDRange::setRange({minValue}, {maxValue})\n" "\tRange must be 0..99\n" - "\tand minValue must not be greater than maxValue" % (minValue, maxValue)) + "\tand minValue must not be greater than maxValue") return self.slider.setRange(minValue, maxValue) diff --git a/examples/widgets/tutorials/cannon/t14.py b/examples/widgets/tutorials/cannon/t14.py index 19fb95ac8..420eeaa08 100644 --- a/examples/widgets/tutorials/cannon/t14.py +++ b/examples/widgets/tutorials/cannon/t14.py @@ -96,9 +96,9 @@ class LCDRange(QtWidgets.QWidget): def setRange(self, minValue, maxValue): if minValue < 0 or maxValue > 99 or minValue > maxValue: - QtCore.qWarning("LCDRange::setRange(%d, %d)\n" + QtCore.qWarning(f"LCDRange::setRange({minValue}, {maxValue})\n" "\tRange must be 0..99\n" - "\tand minValue must not be greater than maxValue" % (minValue, maxValue)) + "\tand minValue must not be greater than maxValue") return self.slider.setRange(minValue, maxValue) diff --git a/examples/widgets/tutorials/cannon/t8.py b/examples/widgets/tutorials/cannon/t8.py index 3761b4353..e2d013eca 100644 --- a/examples/widgets/tutorials/cannon/t8.py +++ b/examples/widgets/tutorials/cannon/t8.py @@ -77,9 +77,9 @@ class LCDRange(QtWidgets.QWidget): def setRange(self, minValue, maxValue): if minValue < 0 or maxValue > 99 or minValue > maxValue: - QtCore.qWarning("LCDRange.setRange(%d, %d)\n" + QtCore.qWarning("LCDRange.setRange({minValue}, {maxValue})\n" "\tRange must be 0..99\n" - "\tand minValue must not be greater than maxValue" % (minValue, maxValue)) + "\tand minValue must not be greater than maxValue") return self.slider.setRange(minValue, maxValue) @@ -111,7 +111,7 @@ class CannonField(QtWidgets.QWidget): def paintEvent(self, event): painter = QtGui.QPainter(self) - painter.drawText(200, 200, "Angle = %d" % self.currentAngle) + painter.drawText(200, 200, f"Angle = {self.currentAngle}") class MyWidget(QtWidgets.QWidget): diff --git a/examples/widgets/tutorials/cannon/t9.py b/examples/widgets/tutorials/cannon/t9.py index ca949c1ac..6d743357d 100644 --- a/examples/widgets/tutorials/cannon/t9.py +++ b/examples/widgets/tutorials/cannon/t9.py @@ -77,9 +77,9 @@ class LCDRange(QtWidgets.QWidget): def setRange(self, minValue, maxValue): if minValue < 0 or maxValue > 99 or minValue > maxValue: - QtCore.qWarning("LCDRange::setRange(%d, %d)\n" + QtCore.qWarning(f"LCDRange::setRange({minValue}, {maxValue})\n" "\tRange must be 0..99\n" - "\tand minValue must not be greater than maxValue" % (minValue, maxValue)) + "\tand minValue must not be greater than maxValue") return self.slider.setRange(minValue, maxValue) diff --git a/examples/xml/dombookmarks/dombookmarks.py b/examples/xml/dombookmarks/dombookmarks.py index 0a8d01ec4..91aaf78d4 100644 --- a/examples/xml/dombookmarks/dombookmarks.py +++ b/examples/xml/dombookmarks/dombookmarks.py @@ -70,8 +70,9 @@ class MainWindow(QtWidgets.QMainWindow): inFile = QtCore.QFile(fileName) if not inFile.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text): + reason = inFile.errorString() QtWidgets.QMessageBox.warning(self, "DOM Bookmarks", - "Cannot read file %s:\n%s." % (fileName, inFile.errorString())) + f"Cannot read file {fileName}:\n{reason}.") return if self.xbelTree.read(inFile): @@ -87,8 +88,9 @@ class MainWindow(QtWidgets.QMainWindow): outFile = QtCore.QFile(fileName) if not outFile.open(QtCore.QFile.WriteOnly | QtCore.QFile.Text): + reason = outFile.errorString() QtWidgets.QMessageBox.warning(self, "DOM Bookmarks", - "Cannot write file %s:\n%s." % (fileName, outFile.errorString())) + "Cannot write file {fileName}:\n{reason}.") return if self.xbelTree.write(outFile): @@ -151,7 +153,7 @@ class XbelTree(QtWidgets.QTreeWidget): ok, errorStr, errorLine, errorColumn = self.domDocument.setContent(device, True) if not ok: QtWidgets.QMessageBox.information(self.window(), "DOM Bookmarks", - "Parse error at line %d, column %d:\n%s" % (errorLine, errorColumn, errorStr)) + f"Parse error at line {errorLine}, column {errorColumn}:\n{errorStr}") return False root = self.domDocument.documentElement() @@ -215,7 +217,7 @@ class XbelTree(QtWidgets.QTreeWidget): item.setText(0, title) folded = (element.attribute('folded') != 'no') - self.setItemExpanded(item, not folded) + item.setExpanded(not folded) child = element.firstChildElement() while not child.isNull(): |