• <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, 評論 - 8, 引用 - 0
            數據加載中……

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

            在 PyQt4 中的菜單和工具欄

            在本部分中,我們將要創建菜單和工具欄。菜單就是在菜單欄中的一組命令。工具欄就是一組常用命令的按鈕。

            主窗口

            QtGui.QMainWindow 類提供了一個應用的主窗口。這使得我們可以創建典型的應用框架,包括狀態欄,工具欄和菜單。

            狀態欄

            狀態欄主要用于顯示狀態信息。

            #!/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()
            

            狀態欄由 QtGui.QMainWindow 幫忙創建。

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

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

            菜單欄

            菜單欄是 GUI 應用中很常用的一部分。它是在多個菜單中命令的集合。在 console 應用中,我們需要記住命令和它們的選項。而這里,我們把很多命令按照邏輯進行分組。這就使得學習使用一個新的應用的時間可以減少。

            #!/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()
            

            在上面的例子中,我們創建了只有一個菜單的菜單欄。這個菜單中包含了一個 action ,如果選中后就會終止應用。我們還創建了一個狀態欄。而且也可以用快捷鍵 Ctrl + Q 退出。

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

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

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

            當我們選擇了這個特定的 action ,觸發的信號就被發送了。這個信號和 QtGui.QApplicationquit() 方法聯系在一起。這就終止了應用。

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

            此處三行,創建了一個菜單欄。我們往菜單欄中添加了一個名為 File 的菜單,而且,我們把 Alt + F 設為了快捷方式。然后我們再把 exitAction 放到了 fileMenu 中。

            工具欄

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

            #!/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()
            

            在上面的例子中,我們創建了一個簡單的工具欄。這個工具欄有一個退出的 action ,當觸發時,就會終止應用。

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

            和前面菜單欄的例子一樣,我們也創建了一個 action 對象。這個對象有一個標簽,圖標以及快捷方式。而且 QtGui.QMainWindowquit() 方法和其觸發信號關聯了起來。

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

            這里,我們創建了工具欄,并把 action 對象放入。

            放到一起

            本節的最后,我們將創建菜單欄,工具欄和狀態欄。而且也會創建一個居中的 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()
            

            這段代碼創建了一個有菜單欄,工具欄及狀態欄典型的 GUI 應用。

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

            這里,我們創建了一個 text edit 的 widget 。我們把其設為 central widget。central widget會占用所有的空間。


            在本部分,我們學習了菜單,工具欄,狀態欄和主應用窗口。

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

            評論

            # re: [Python][PyQt4]在 PyQt4 中的菜單和工具欄[未登錄]  回復  更多評論   

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

            # re: [Python][PyQt4]在 PyQt4 中的菜單和工具欄  回復  更多評論   

            簡介及哈哈哈哈哈哈哈哈哈哈和
            2015-11-19 10:55 | 方法從
            国产91色综合久久免费| 久久人人爽人人澡人人高潮AV | 三上悠亚久久精品| 久久九九久精品国产| 亚洲美日韩Av中文字幕无码久久久妻妇 | 亚洲伊人久久大香线蕉苏妲己| 漂亮人妻被中出中文字幕久久| 九九热久久免费视频| 欧美午夜A∨大片久久| 午夜人妻久久久久久久久| 91麻精品国产91久久久久| 久久人妻AV中文字幕| 亚洲狠狠久久综合一区77777 | 久久精品国产亚洲AV无码偷窥| 国产高潮国产高潮久久久| 国产亚州精品女人久久久久久| 久久精品国产精品亚洲| 日本国产精品久久| 精品无码久久久久国产| 久久99精品国产99久久6男男| 久久精品国产亚洲av日韩| 精品久久一区二区三区| 久久亚洲精品国产亚洲老地址| 久久久久久久久久久精品尤物| 九九99精品久久久久久| 亚洲Av无码国产情品久久| 久久99精品久久久久久久不卡| 99久久中文字幕| 久久久久人妻精品一区三寸蜜桃| 亚洲欧美成人综合久久久 | 国产L精品国产亚洲区久久 | 一本一本久久a久久综合精品蜜桃 一本一道久久综合狠狠老 | 国产精品美女久久久网AV| 人妻无码久久一区二区三区免费| 欧美日韩精品久久久免费观看| 久久99精品久久久久久| 男女久久久国产一区二区三区| 亚洲欧美日韩精品久久| 99久久99这里只有免费费精品| 久久综合88熟人妻| 97精品伊人久久久大香线蕉|