Редактирование: PyQt FAQ Menus and Toolbars

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск
Внимание: Вы не представились системе. Ваш IP-адрес будет записан в историю изменений этой страницы.
Правка может быть отменена. Пожалуйста, просмотрите сравнение версий, чтобы убедиться, что это именно те изменения, которые вас интересуют, и нажмите «Записать страницу», чтобы изменения вступили в силу.
Текущая версия Ваш текст
Строка 1: Строка 1:
 +
== Main Window ==
 +
The QMainWindow class provides a main application window. This enables to create the classic application skeleton with a statusbar, toolbars and a menubar.
 +
== Statusbar ==
 +
The statusbar is a widget that si used for displaying status information.
 +
<source lang="python">
 +
#!/usr/bin/python
 +
 +
# statusbar.py
 +
 +
import sys
 +
from PyQt4 import QtGui
 +
 +
class MainWindow(QtGui.QMainWindow):
 +
    def __init__(self):
 +
        QtGui.QMainWindow.__init__(self)
 +
 +
        self.resize(250, 150)
 +
        self.setWindowTitle('statusbar')
 +
 +
        self.statusBar().showMessage('Ready')
 +
 +
 +
app = QtGui.QApplication(sys.argv)
 +
main = MainWindow()
 +
main.show()
 +
sys.exit(app.exec_())
 +
</source>
 +
 +
<source lang="python">
 +
self.statusBar().showMessage('Ready')
 +
</source>
 +
 +
To get the statusbar, we call the <i>statusBar()</i> method of the QApplication class. The <i>showMessage()</i> displays message on the statusbar.
 +
 +
== Menubar ==
 +
A menubar is one of the most visible parts of the GUI application. It is a group of commands located in various menus. While in console applications you had to remember all those arcane commands, here we have most of the commands grouped into logical parts. There are accepted standards that further reduce the amount of time spending to learn a new application.
 +
<source lang="python">
 +
#!/usr/bin/python
 +
 +
# menubar.py
 +
 +
import sys
 +
from PyQt4 import QtGui, QtCore
 +
 +
class MainWindow(QtGui.QMainWindow):
 +
    def __init__(self):
 +
        QtGui.QMainWindow.__init__(self)
 +
 +
        self.resize(250, 150)
 +
        self.setWindowTitle('menubar')
 +
 +
        exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
 +
        exit.setShortcut('Ctrl+Q')
 +
        exit.setStatusTip('Exit application')
 +
        self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
 +
 +
        self.statusBar()
 +
 +
        menubar = self.menuBar()
 +
        file = menubar.addMenu('&File')
 +
        file.addAction(exit)
 +
 +
app = QtGui.QApplication(sys.argv)
 +
main = MainWindow()
 +
main.show()
 +
sys.exit(app.exec_())
 +
</source>
 +
 +
<source lang="python">
 +
menubar = self.menuBar()
 +
file = menubar.addMenu('&File')
 +
file.addAction(exit)
 +
</source>
 +
 +
First we create a menubar with the <i>menuBar()</i> method of the <i>QMainWindow</i> class. Then we add a menu with the <i>AddMenu()</i> method. In the end we plug the action object into the file menu.
 +
 +
== Toolbar ==
 +
Menus group all commands that we can use in an application. Toolbars provide a quick access to the most frequently used commands.
 +
<source lang="python">
 +
#!/usr/bin/python
 +
 +
# toolbar.py
 +
 +
import sys
 +
from PyQt4 import QtGui, QtCore
 +
 +
class MainWindow(QtGui.QMainWindow):
 +
    def __init__(self):
 +
        QtGui.QMainWindow.__init__(self)
 +
 +
        self.resize(250, 150)
 +
        self.setWindowTitle('toolbar')
 +
 +
        self.exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
 +
        self.exit.setShortcut('Ctrl+Q')
 +
        self.connect(self.exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
 +
 +
        self.toolbar = self.addToolBar('Exit')
 +
        self.toolbar.addAction(self.exit)
 +
 +
 +
app = QtGui.QApplication(sys.argv)
 +
main = MainWindow()
 +
main.show()
 +
sys.exit(app.exec_())
 +
</source>
 +
 +
<source lang="python">
 +
self.exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
 +
self.exit.setShortcut('Ctrl+Q')
 +
</source>
 +
GUI applications are controlled with commands. These commands can be launched from a menu, a context menu, a toolbar or with a shortcut. PyQt simplifies development with the introduction of <b>actions</b>. An action object can have menu text, an icon, a shortcut, status text, "What's This?" text and a tooltip. In our example, we define an action object with an icon, a tooltip and a shortcut.
 +
<source lang="python">
 +
self.connect(self.exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
 +
</source>
 +
 +
Here we connect the action's <i>triggered()</i> signal to the predefined <i>close()</i> signal.
 +
 +
<source lang="python">
 +
self.toolbar = self.addToolBar('Exit')
 +
self.toolbar.addAction(self.exit)
 +
</source>
 +
 +
Here we create a toolbar and plug and action object into it.
 +
 +
[[image: pyqt_faq_toolbar.jpg | center]]
 +
 +
== Putting it together ==
 +
In the last example of this section, we will create a menubar, toolbar and a statusbar. We will also create a central widget.
 +
<source lang="python">
 +
#!/usr/bin/python
 +
 +
# mainwindow.py
 +
 +
import sys
 +
from PyQt4 import QtGui, QtCore
 +
 +
class MainWindow(QtGui.QMainWindow):
 +
    def __init__(self):
 +
        QtGui.QMainWindow.__init__(self)
 +
 +
        self.resize(350, 250)
 +
        self.setWindowTitle('mainwindow')
 +
 +
        textEdit = QtGui.QTextEdit()
 +
        self.setCentralWidget(textEdit)
 +
 +
        exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self)
 +
        exit.setShortcut('Ctrl+Q')
 +
        exit.setStatusTip('Exit application')
 +
        self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
 +
 +
        self.statusBar()
 +
 +
        menubar = self.menuBar()
 +
        file = menubar.addMenu('&File')
 +
        file.addAction(exit)
 +
 +
        toolbar = self.addToolBar('Exit')
 +
        toolbar.addAction(exit)
 +
 +
 +
app = QtGui.QApplication(sys.argv)
 +
main = MainWindow()
 +
main.show()
 +
sys.exit(app.exec_())
 +
</source>
 +
 +
<source lang="python">
 +
textEdit = QtGui.QTextEdit()
 +
self.setCentralWidget(textEdit)
 +
</source>
 +
 +
Here we create a text edit widget. We set it to be the central widget of the <i>QMainWindow</i>. The central widget will occupy all space that is left.
 +
 +
[[image: pyqt_faq_mainwindow.jpg | center]]
 +
 +
[[Категория:Qt]]
 +
[[Категория:Python]]

Пожалуйста, обратите внимание, что все ваши добавления могут быть отредактированы или удалены другими участниками. Если вы не хотите, чтобы кто-либо изменял ваши тексты, не помещайте их сюда.
Вы также подтверждаете, что являетесь автором вносимых дополнений, или скопировали их из источника, допускающего свободное распространение и изменение своего содержимого (см. Wiki.crossplatform.ru:Авторское право). НЕ РАЗМЕЩАЙТЕ БЕЗ РАЗРЕШЕНИЯ ОХРАНЯЕМЫЕ АВТОРСКИМ ПРАВОМ МАТЕРИАЛЫ!