• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 41, 文章 - 8, 評(píng)論 - 8, 引用 - 0
            數(shù)據(jù)加載中……

            [Python][PyQt4]在 PyQt4 中的菜單和工具欄

            在 PyQt4 中的菜單和工具欄

            在本部分中,我們將要?jiǎng)?chuàng)建菜單和工具欄。菜單就是在菜單欄中的一組命令。工具欄就是一組常用命令的按鈕。

            主窗口

            QtGui.QMainWindow 類提供了一個(gè)應(yīng)用的主窗口。這使得我們可以創(chuàng)建典型的應(yīng)用框架,包括狀態(tài)欄,工具欄和菜單。

            狀態(tài)欄

            狀態(tài)欄主要用于顯示狀態(tài)信息。

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            This program creates a statusbar.
            author: Jan Bodnar
            website: zetcode.com
            last edited: September 2011
            """
            import sys
            from PyQt4 import QtGui
            class Example(QtGui.QMainWindow):
                def __init__(self):
                    super(Example, self).__init__()
                    self.initUI()
                def initUI(self):
                    self.statusBar().showMessage('Ready')
                    self.setGeometry(300, 300, 250, 150)
                    self.setWindowTitle('Statusbar')
                    self.show()
            def main():
                app = QtGui.QApplication(sys.argv)
                ex = Example()
                sys.exit(app.exec_())
            if __name__ == '__main__':
                main()
            

            狀態(tài)欄由 QtGui.QMainWindow 幫忙創(chuàng)建。

            self.statusBar().showMessage('Ready')
            

            為了得到狀態(tài)欄,我們調(diào)用了 QtGui.QMainWindowstatusBar() 方法。第一次調(diào)用創(chuàng)建了狀態(tài)欄,隨后返回 statusbar 對(duì)象。接著我們調(diào)用 showMessage 在狀態(tài)欄上顯示了一條消息。

            菜單欄

            菜單欄是 GUI 應(yīng)用中很常用的一部分。它是在多個(gè)菜單中命令的集合。在 console 應(yīng)用中,我們需要記住命令和它們的選項(xiàng)。而這里,我們把很多命令按照邏輯進(jìn)行分組。這就使得學(xué)習(xí)使用一個(gè)新的應(yīng)用的時(shí)間可以減少。

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            This program creates a menubar. The
            menubar has one menu with an exit action.
            author: Jan Bodnar
            website: zetcode.com
            last edited: August 2011
            """
            import sys
            from PyQt4 import QtGui
            class Example(QtGui.QMainWindow):
                def __init__(self):
                    super(Example, self).__init__()
                    self.initUI()
                def initUI(self):
                    exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self)
                    exitAction.setShortcut('Ctrl+Q')
                    exitAction.setStatusTip('Exit application')
                    exitAction.triggered.connect(QtGui.qApp.quit)
                    self.statusBar()
                    menubar = self.menuBar()
                    fileMenu = menubar.addMenu('&File')
                    fileMenu.addAction(exitAction)
                    self.setGeometry(300, 300, 300, 200)
                    self.setWindowTitle('Menubar')
                    self.show()
            def main():
                app = QtGui.QApplication(sys.argv)
                ex = Example()
                sys.exit(app.exec_())
            if __name__ == '__main__':
                main()
            

            在上面的例子中,我們創(chuàng)建了只有一個(gè)菜單的菜單欄。這個(gè)菜單中包含了一個(gè) action ,如果選中后就會(huì)終止應(yīng)用。我們還創(chuàng)建了一個(gè)狀態(tài)欄。而且也可以用快捷鍵 Ctrl + Q 退出。

            exitAction = QtGui.QAction(QtGui.QIcon('exit.png'))
            exitAction.setShortcut('Ctrl+Q')
            exitAction.setStatusTip('Exit application')
            

            QtGui.QAction 是一個(gè) action 的抽象,包括菜單欄,工具欄或者是自定義的快捷鍵。在上面的三行,我們創(chuàng)建了一個(gè) action ,有一個(gè)指定的圖標(biāo)以及 ‘Exit’ 標(biāo)簽。而且,這個(gè) action 定義了快捷鍵。第三行則是創(chuàng)建一個(gè)提示,當(dāng)我們把鼠標(biāo)指針移到菜單條目上,將在狀態(tài)欄中顯示相應(yīng)的提示。

            exitAction.triggered.connect(QtGui.qApp.quit)
            

            當(dāng)我們選擇了這個(gè)特定的 action ,觸發(fā)的信號(hào)就被發(fā)送了。這個(gè)信號(hào)和 QtGui.QApplicationquit() 方法聯(lián)系在一起。這就終止了應(yīng)用。

            menubar = self.menuBar()
            fileMenu = menubar.addMenu('&File')
            fileMenu.addAction(exitAction)
            

            此處三行,創(chuàng)建了一個(gè)菜單欄。我們往菜單欄中添加了一個(gè)名為 File 的菜單,而且,我們把 Alt + F 設(shè)為了快捷方式。然后我們?cè)侔?exitAction 放到了 fileMenu 中。

            工具欄

            在一個(gè)應(yīng)用中,菜單把所有的命令分組。而工具欄中則提供了常用命令的快捷方式。

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            This program creates a toolbar.
            The toolbar has one action, which
            terminates the application, if triggered.
            author: Jan Bodnar
            website: zetcode.com
            last edited: September 2011
            """
            import sys
            from PyQt4 import QtGui
            class Example(QtGui.QMainWindow):
                def __init__(self):
                    super(Example, self).__init__()
                    self.initUI()
                def initUI(self):
                    exitAction = QtGui.QAction(QtGui.QIcon('exit24.png'), 'Exit', self)
                    exitAction.setShortcut('Ctrl+Q')
                    exitAction.triggered.connect(QtGui.qApp.quit)
                    self.toolbar = self.addToolBar('Exit')
                    self.toolbar.addAction(exitAction)
                    self.setGeometry(300, 300, 300, 200)
                    self.setWindowTitle('Toolbar')
                    self.show()
            def main():
                app = QtGui.QApplication(sys.argv)
                ex = Example()
                sys.exit(app.exec_())
            if __name__ == '__main__':
                main()
            

            在上面的例子中,我們創(chuàng)建了一個(gè)簡(jiǎn)單的工具欄。這個(gè)工具欄有一個(gè)退出的 action ,當(dāng)觸發(fā)時(shí),就會(huì)終止應(yīng)用。

            exitAction = QtGui.QAction(QtGui.QIcon('exit24.png'), 'Exit', self)
            exitAction.setShortcut('Ctrl+Q')
            exitAction.triggered.connect(QtGui.qApp.quit)
            

            和前面菜單欄的例子一樣,我們也創(chuàng)建了一個(gè) action 對(duì)象。這個(gè)對(duì)象有一個(gè)標(biāo)簽,圖標(biāo)以及快捷方式。而且 QtGui.QMainWindowquit() 方法和其觸發(fā)信號(hào)關(guān)聯(lián)了起來(lái)。

            self.toolbar = self.addToolBar('Exit')
            self.toolbar.addAction(exitAction)
            

            這里,我們創(chuàng)建了工具欄,并把 action 對(duì)象放入。

            放到一起

            本節(jié)的最后,我們將創(chuàng)建菜單欄,工具欄和狀態(tài)欄。而且也會(huì)創(chuàng)建一個(gè)居中的 widget 。

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            This program creates a skeleton of
            a classic GUI application with a menubar,
            toolbar, statusbar and a central widget.
            author: Jan Bodnar
            website: zetcode.com
            last edited: September 2011
            """
            import sys
            from PyQt4 import QtGui
            class Example(QtGui.QMainWindow):
                def __init__(self):
                    super(Example, self).__init__()
                    self.initUI()
                def initUI(self):
                    textEdit = QtGui.QTextEdit()
                    self.setCentralWidget(textEdit)
                    exitAction = QtGui.QAction(QtGui.QIcon('exit24.png'), 'Exit', self)
                    exitAction.setShortcut('Ctrl+Q')
                    exitAction.setStatusTip('Exit application')
                    exitAction.triggered.connect(self.close)
                    self.statusBar()
                    menubar = self.menuBar()
                    fileMenu = menubar.addMenu('&File')
                    fileMenu.addAction(exitAction)
                    toolbar = self.addToolBar('Exit')
                    toolbar.addAction(exitAction)
                    self.setGeometry(300, 300, 350, 250)
                    self.setWindowTitle('Main window')
                    self.show()
            def main():
                app = QtGui.QApplication(sys.argv)
                ex = Example()
                sys.exit(app.exec_())
            if __name__ == '__main__':
                main()
            

            這段代碼創(chuàng)建了一個(gè)有菜單欄,工具欄及狀態(tài)欄典型的 GUI 應(yīng)用。

            textEdit = QtGui.QTextEdit()
            self.setCentralWidget(textEdit)
            

            這里,我們創(chuàng)建了一個(gè) text edit 的 widget 。我們把其設(shè)為 central widget。central widget會(huì)占用所有的空間。


            在本部分,我們學(xué)習(xí)了菜單,工具欄,狀態(tài)欄和主應(yīng)用窗口。

            posted on 2012-02-05 10:03 mirguest 閱讀(6201) 評(píng)論(2)  編輯 收藏 引用 所屬分類: Python

            評(píng)論

            # re: [Python][PyQt4]在 PyQt4 中的菜單和工具欄[未登錄](méi)  回復(fù)  更多評(píng)論   

            我想問(wèn)題下,QMainWindow 跟 QWidget 兩個(gè)怎么搭配用, 也就是說(shuō),那些widget里面的東西怎么跟mainwindow放一塊去?
            2013-04-04 15:16 | 西丁

            # re: [Python][PyQt4]在 PyQt4 中的菜單和工具欄  回復(fù)  更多評(píng)論   

            簡(jiǎn)介及哈哈哈哈哈哈哈哈哈哈和
            2015-11-19 10:55 | 方法從
            99久久99久久精品国产片| 久久精品亚洲欧美日韩久久| 久久亚洲国产最新网站| 精品国产日韩久久亚洲| 久久w5ww成w人免费| AAA级久久久精品无码区| 久久久精品无码专区不卡| 日韩AV无码久久一区二区| 国产精品免费久久| 无码伊人66久久大杳蕉网站谷歌| 久久精品国产福利国产秒| 久久免费大片| 精产国品久久一二三产区区别| 久久国产亚洲精品无码| 久久久久国色AV免费观看| www.久久精品| 久久久久精品国产亚洲AV无码| 93精91精品国产综合久久香蕉| 2021国内久久精品| 久久成人18免费网站| 久久99精品久久久久久久不卡 | 久久综合噜噜激激的五月天| 国产精品免费福利久久| 狠狠精品久久久无码中文字幕| 久久久久久曰本AV免费免费| 久久黄视频| 国产午夜福利精品久久| 久久久青草青青亚洲国产免观| 久久天天躁狠狠躁夜夜躁2O2O | 久久综合九色综合欧美就去吻| 国产精品女同久久久久电影院| 久久91精品国产91| 久久精品免费大片国产大片| 青青青青久久精品国产| 久久99中文字幕久久| 91精品国产乱码久久久久久| 国产精品久久久福利| 久久综合九色综合97_久久久| 精品久久久久久国产91| 99久久人人爽亚洲精品美女| 久久亚洲2019中文字幕|