Python menus and toolbars sample application

上级 705d8c02
The MIT License (MIT)
Copyright (c) 2014 Prahlad Yeri
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
# Sample PyQt Application
A sample GUI application that shows how to create and use menus, toolbars, and status bars using Python and PyQt.
## Author
Leodanis Pozo Ramos – [@lpozo78](https://twitter.com/lpozo78) – lpozor78@gmail.com
## License
- This application is distributed under the MIT license. See `LICENSE` for details.
- The set of icons used in this application are part of the [TurkinOS](https://github.com/llamaret/turkinos-icon) icon theme, which is distributed under the [GPL v3.0 license](https://github.com/llamaret/turkinos-icon/blob/master/LICENSE). See `resources/LICENSE` for details.
\ No newline at end of file
此差异已折叠。
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file alias="file-new.svg">resources/file-new.svg</file>
<file alias="file-open.svg">resources/file-open.svg</file>
<file alias="file-save.svg">resources/file-save.svg</file>
<file alias="file-exit.svg">resources/file-exit.svg</file>
<file alias="edit-copy.svg">resources/edit-copy.svg</file>
<file alias="edit-cut.svg">resources/edit-cut.svg</file>
<file alias="edit-paste.svg">resources/edit-paste.svg</file>
<file alias="help-content.svg">resources/help-content.svg</file>
</qresource>
</RCC>
此差异已折叠。
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 22">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#4d4d4d;
}
</style>
</defs>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="m14.999993 6.0000017h-10.999988c-.5520048 0-1.000005.4480002-1.000005.9999916v11.0000017c0 .552005.4480002 1.000005 1.000005 1.000005h10.999988c.552005 0 1.000005-.448 1.000005-1.000005v-11.0000017c0-.5519914-.448-.9999916-1.000005-.9999916zm3.000002-3.0000017h-9.9999967c-.5520048 0-1.000005.4480002-1.000005 1.000005v.9999916h1.000005v-.5000025c0-.2759957.2240068-.4999891.5000025-.4999891h8.9999912c.27601 0 .500003.2239934.500003.4999891v9.0000049c0 .275996-.223993.500003-.500003.500003h-.499989v.999991h.999992c.552005 0 1.000005-.448 1.000005-.999991v-9.999997c0-.5520048-.448-1.000005-1.000005-1.000005z"
class="ColorScheme-Text" />
</svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 22">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#4d4d4d;
}
</style>
</defs>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="m7.0136719 2-.4355469.7753906c-.2699231.4812667-.5038694.7087891-.6289062 1.1367188-.0625185.2139647-.0612584.4699226.0019531.7011718.0632171.2312494.1726285.4527981.3261719.7265626l3.3945312 6.0625002-1.7148438 3.023437c-.4483616-.277461-.8911737-.591797-1.4550781-.591797-1.6582865 0-3.0019531 1.388975-3.0019531 3.083985 0 1.695008 1.3436663 3.082031 3.0019531 3.082031 1.658287 0 3.0019532-1.387023 3.0019531-3.082031 0-.133955-.0209824-.259363-.0371093-.386719.4622484-1.003569.8985411-1.788627 1.8085941-2.248047.003037-.003375.002934-.004437.005859-.007812l1.253906 2.238281c-.017223.134034-.039062.265801-.039062.404297 0 1.695008 1.343665 3.082031 3.001953 3.082031 1.658287 0 3.001953-1.387023 3.001953-3.082031 0-1.69501-1.343667-3.083985-3.001953-3.083985-.56176 0-1.004412.308543-1.451172.583985l-1.708984-3.015625 3.398437-6.0625002v-.0019532c.152934-.2728193.261159-.4939397.324219-.7246094.063214-.2312492.066332-.4872071.003906-.7011718-.125138-.4278882-.358984-.6554521-.628906-1.1367188l-.435547-.7753906-3.994141 7.0488281zm-.5117188 13.884766c.5564078 0 .9960938.448401.9960938 1.033203s-.439686 1.03125-.9960938 1.03125-.9941406-.446448-.9941406-1.03125c0-.584803.4377328-1.033203.9941406-1.033203zm8.9960939 0c .556407 0 .99414.448401.994141 1.033203 0 .584802-.437734 1.03125-.994141 1.03125-.556409 0-.996094-.446448-.996094-1.03125 0-.584803.439685-1.033203.996094-1.033203z"
class="ColorScheme-Text" />
</svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 22">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#4d4d4d;
}
</style>
</defs>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="m14.12662 8.00521h-.9452v-2.22935c0-.23294-.18815-.4217-.42014-.4217h-.45273l-.90589-3.0529c-.053-.17871-.21679-.30126-.40266-.30126-.18578 0-.34963.12255-.40266.30126l-.90589 3.0529h-.45273c-.23199 0-.42014.18876-.42014.4217v2.22942h-.9452c-1.03295 0-1.87338.84348-1.87338 1.88035v8.23403c0 1.03686.84035 1.88034 1.87338 1.88034h6.25324c1.03295 0 1.87338-.84348 1.87338-1.88034v-8.23403c0-1.03696-.84035-1.88042-1.87338-1.88042z"
class="ColorScheme-Text" />
</svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 22">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#4d4d4d;
}
</style>
</defs>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="m17.927299 6.98434c-.715348-1.22562-1.685765-2.19604-2.911381-2.91138-1.225871-.71535-2.564127-1.07296-4.015662-1.07296-1.4514075 0-2.7900468.35761-4.0156625 1.07296-1.2257436.71522-2.1961612 1.68563-2.9115085 2.91138-.7154752 1.22574-1.073085 2.56438-1.073085 4.01566 0 1.45141.3577376 2.78979 1.0729571 4.01566.7153474 1.22549 1.685765 2.19604 2.9115085 2.91138 1.2257436.71535 2.5642551 1.07296 4.0156624 1.07296 1.451407 0 2.790046-.35761 4.015662-1.07296 1.225616-.71522 2.196033-1.68576 2.911381-2.91138.715219-1.22574 1.072829-2.56438 1.072829-4.01566 0-1.45141-.35761-2.79005-1.072701-4.01566zm-3.156351 5.90101c.131946.13181.198047.28831.198047.46884 0 .18743-.0661.34725-.198047.47907l-.937431.93756c-.131946.13195-.291637.19792-.4792.19792-.18053 0-.337025-.066-.468715-.19792l-1.885474-1.88547-1.8854742 1.88547c-.1319462.13195-.2881847.19792-.4687156.19792-.1875629 0-.3472535-.066-.4791996-.19792l-.9375589-.93756c-.1319462-.13182-.1979192-.29164-.1979192-.47907 0-.18053.065973-.33703.1979192-.46884l1.8854741-1.88535-1.8854741-1.88547c-.1319462-.13182-.1979192-.28819-.1979192-.46872 0-.18756.065973-.34725.1979192-.4792l.9375589-.93743c.1319461-.13195.2916367-.19792.4791996-.19792.1805309 0 .3367694.066.4687156.19792l1.8854742 1.88535 1.885474-1.88535c.13169-.13195.288185-.19792.468715-.19792.187563 0 .347254.066.4792.19792l.937431.93743c.131946.13195.198047.29164.198047.4792 0 .18053-.0661.3369-.198047.46872l-1.885346 1.88547z"
class="ColorScheme-Text" />
</svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 22">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#4d4d4d;
}
</style>
</defs>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="m9.666667 3c-.246249 0-.444445.1981956-.444445.4444444v5.7777776h-5.7777776c-.2462222 0-.4444444.198222-.4444444.444445v2.666666c0 .246223.1982222.444445.4444444.444445h5.7777776v5.777778c0 .246248.198196.444444.444445.444444h2.666666c.246249 0 .444445-.198196.444445-.444444v-5.777778h5.777778c.246222 0 .444444-.198222.444444-.444445v-2.666666c0-.246223-.198222-.444445-.444444-.444445h-5.777778v-5.7777776c0-.2462488-.198196-.4444444-.444445-.4444444z"
class="ColorScheme-Text" />
</svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 22">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#4d4d4d;
}
</style>
</defs>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="m2.5000001 1034.3622c-.277 0-.5.223-.5.5v1.9609 1.0391 10.0391c0 .2557.2052456.4609.4609375.4609h17.0781244c.255692 0 .460938-.2052.460938-.4609v-11.0782c0-.2557-.205246-.4609-.460938-.4609h-8.181159c-.569865.017-.846295-.1321-1.097628-.3681l-1.1884019-1.21c-.16805-.2202-.294873-.4219-.571873-.4219z"
class="ColorScheme-Text"
transform="matrix(.88888889 0 0 .88888889 1.222222 -914.87751)"/>
</svg>
<svg viewBox="0 0 22 22" xmlns="http://www.w3.org/2000/svg">
<defs id="defs">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text { color: #4d4d4d; }
</style>
</defs>
<path style="fill:currentColor;fill-opacity;stroke:none" class="ColorScheme-Text" d="m3.6679688 3c-.3691339 0-.6679688.2988749-.6679688.6679688v14.6640622c0 .369134.2988349.667969.6679688.667969h14.6640622c.369134 0 .667969-.298835.667969-.667969v-11.5585935c0-.1772133-.069939-.3473157-.195312-.4726563l-3.105469-3.1054687c-.12534-.1253333-.295351-.1953125-.472657-.1953125zm1.9707031 1.5273438h9.1777341v4.9003906h-9.1777341zm5.5703121.4726562c-.115073 0-.208984.1073944-.208984.2402344v3.5195312c0 .13288.093911.2402344.208984.2402344h1.582032c.115073 0 .208984-.1073544.208984-.2402344v-3.5195312c0-.13284-.093911-.2402344-.208984-.2402344z" transform="matrix(1.000000005 0 0 1.000000005 .000001275238 .00000172266)"/></svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 22">
<defs id="defs3051">
<style type="text/css" id="current-color-scheme">
.ColorScheme-Text {
color:#4d4d4d;
}
</style>
</defs>
<path
style="fill:currentColor;fill-opacity:1;stroke:none"
d="m10.999999 3.000002a8 8 0 0 0 -8 8 8 8 0 0 0 8 8 8 8 0 0 0 8-8 8 8 0 0 0 -8-8zm-.199219 3.5a1 1 0 0 1 1 1 1 1 0 0 1 -1 1 1 1 0 0 1 -.9999998-1 1 1 0 0 1 .9999998-1zm-.107422 3h .212891c.356143 0 .644531.2864351.644531.642578v4.714844c0 .356143-.288388.642578-.644531.642578h-.212891c-.356143 0-.642578-.286435-.642578-.642578v-4.714844c0-.3561429.286435-.642578.642578-.642578z"
class="ColorScheme-Text" />
</svg>
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Sample PyQt application."""
import sys
from functools import partial
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QKeySequence
from PyQt5.QtWidgets import (
QAction,
QApplication,
QLabel,
QMainWindow,
QMenu,
QSpinBox,
QToolBar,
)
import qrc_resources
class Window(QMainWindow):
"""Main Window."""
def __init__(self, parent=None):
"""Initializer."""
super().__init__(parent)
self.setWindowTitle("Python Menus & Toolbars")
self.resize(400, 200)
self.centralWidget = QLabel("Hello, World")
self.centralWidget.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.setCentralWidget(self.centralWidget)
self._createActions()
self._createMenuBar()
self._createToolBars()
# self._createContextMenu()
self._connectActions()
self._createStatusBar()
def _createMenuBar(self):
menuBar = self.menuBar()
# File menu
fileMenu = QMenu("&File", self)
menuBar.addMenu(fileMenu)
fileMenu.addAction(self.newAction)
fileMenu.addAction(self.openAction)
# Open Recent submenu
self.openRecentMenu = fileMenu.addMenu("Open Recent")
fileMenu.addAction(self.saveAction)
# Separator
fileMenu.addSeparator()
fileMenu.addAction(self.exitAction)
# Edit menu
editMenu = menuBar.addMenu("&Edit")
editMenu.addAction(self.copyAction)
editMenu.addAction(self.pasteAction)
editMenu.addAction(self.cutAction)
# Separator
editMenu.addSeparator()
# Find and Replace submenu
findMenu = editMenu.addMenu("Find and Replace")
findMenu.addAction("Find...")
findMenu.addAction("Replace...")
# Help menu
helpMenu = menuBar.addMenu(QIcon(":help-content.svg"), "&Help")
helpMenu.addAction(self.helpContentAction)
helpMenu.addAction(self.aboutAction)
def _createToolBars(self):
# File toolbar
fileToolBar = self.addToolBar("File")
fileToolBar.setMovable(False)
fileToolBar.addAction(self.newAction)
fileToolBar.addAction(self.openAction)
fileToolBar.addAction(self.saveAction)
# Edit toolbar
editToolBar = QToolBar("Edit", self)
self.addToolBar(editToolBar)
editToolBar.addAction(self.copyAction)
editToolBar.addAction(self.pasteAction)
editToolBar.addAction(self.cutAction)
# Widgets
self.fontSizeSpinBox = QSpinBox()
editToolBar.addWidget(self.fontSizeSpinBox)
def _createStatusBar(self):
self.statusbar = self.statusBar()
# Temporary message
self.statusbar.showMessage("Ready", 3000)
# Permanent widget
self.wcLabel = QLabel(f"{self.getWordCount()} Words")
self.statusbar.addPermanentWidget(self.wcLabel)
def _createActions(self):
# File actions
self.newAction = QAction(self)
self.newAction.setText("&New")
self.newAction.setIcon(QIcon(":file-new.svg"))
self.openAction = QAction(QIcon(":file-open.svg"), "&Open...", self)
self.saveAction = QAction(QIcon(":file-save.svg"), "&Save", self)
self.exitAction = QAction("&Exit", self)
# String-based key sequences
self.newAction.setShortcut("Ctrl+N")
self.openAction.setShortcut("Ctrl+O")
self.saveAction.setShortcut("Ctrl+S")
# Help tips
newTip = "Create a new file"
self.newAction.setStatusTip(newTip)
self.newAction.setToolTip(newTip)
self.newAction.setWhatsThis("Create a new and empty text file")
# Edit actions
self.copyAction = QAction(QIcon(":edit-copy.svg"), "&Copy", self)
self.pasteAction = QAction(QIcon(":edit-paste.svg"), "&Paste", self)
self.cutAction = QAction(QIcon(":edit-cut.svg"), "C&ut", self)
# Standard key sequence
self.copyAction.setShortcut(QKeySequence.Copy)
self.pasteAction.setShortcut(QKeySequence.Paste)
self.cutAction.setShortcut(QKeySequence.Cut)
# Help actions
self.helpContentAction = QAction("&Help Content...", self)
self.aboutAction = QAction("&About...", self)
# Uncomment this method to create a context menu using menu policy
# def _createContextMenu(self):
# # Setting contextMenuPolicy
# self.centralWidget.setContextMenuPolicy(Qt.ActionsContextMenu)
# # Populating the widget with actions
# self.centralWidget.addAction(self.newAction)
# self.centralWidget.addAction(self.openAction)
# self.centralWidget.addAction(self.saveAction)
# self.centralWidget.addAction(self.copyAction)
# self.centralWidget.addAction(self.pasteAction)
# self.centralWidget.addAction(self.cutAction)
def contextMenuEvent(self, event):
# Context menu
menu = QMenu(self.centralWidget)
# Populating the menu with actions
menu.addAction(self.newAction)
menu.addAction(self.openAction)
menu.addAction(self.saveAction)
# Separator
separator = QAction(self)
separator.setSeparator(True)
menu.addAction(separator)
menu.addAction(self.copyAction)
menu.addAction(self.pasteAction)
menu.addAction(self.cutAction)
# Launching the menu
menu.exec(event.globalPos())
def _connectActions(self):
# Connect File actions
self.newAction.triggered.connect(self.newFile)
self.openAction.triggered.connect(self.openFile)
self.saveAction.triggered.connect(self.saveFile)
self.exitAction.triggered.connect(self.close)
# Connect Open Recent to dynamically populate it
self.openRecentMenu.aboutToShow.connect(self.populateOpenRecent)
# Connect Edit actions
self.copyAction.triggered.connect(self.copyContent)
self.pasteAction.triggered.connect(self.pasteContent)
self.cutAction.triggered.connect(self.cutContent)
# Connect Help actions
self.helpContentAction.triggered.connect(self.helpContent)
self.aboutAction.triggered.connect(self.about)
# Slots
def newFile(self):
# Logic for creating a new file goes here...
self.centralWidget.setText("<b>File > New</b> clicked")
def openFile(self):
# Logic for opening an existing file goes here...
self.centralWidget.setText("<b>File > Open...</b> clicked")
def saveFile(self):
# Logic for saving a file goes here...
self.centralWidget.setText("<b>File > Save</b> clicked")
def copyContent(self):
# Logic for copying content goes here...
self.centralWidget.setText("<b>Edit > Copy</b> clicked")
def pasteContent(self):
# Logic for pasting content goes here...
self.centralWidget.setText("<b>Edit > Pate</b> clicked")
def cutContent(self):
# Logic for cutting content goes here...
self.centralWidget.setText("<b>Edit > Cut</b> clicked")
def helpContent(self):
# Logic for launching help goes here...
self.centralWidget.setText("<b>Help > Help Content...</b> clicked")
def about(self):
# Logic for showing an about dialog content goes here...
self.centralWidget.setText("<b>Help > About...</b> clicked")
def populateOpenRecent(self):
# Step 1. Remove the old options from the menu
self.openRecentMenu.clear()
# Step 2. Dynamically create the actions
actions = []
filenames = [f"File-{n}" for n in range(5)]
for filename in filenames:
action = QAction(filename, self)
action.triggered.connect(partial(self.openRecentFile, filename))
actions.append(action)
# Step 3. Add the actions to the menu
self.openRecentMenu.addActions(actions)
def openRecentFile(self, filename):
# Logic for opening a recent file goes here...
self.centralWidget.setText(f"<b>{filename}</b> opened")
def getWordCount(self):
# Logic for computing the word count goes here...
return 42
if __name__ == "__main__":
# Create the application
app = QApplication(sys.argv)
# Create and show the main window
win = Window()
win.show()
# Run the event loop
sys.exit(app.exec_())
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册