python gui QT开发pdf python qt gui编程

您所在的位置:网站首页 setGeometry函数 python gui QT开发pdf python qt gui编程

python gui QT开发pdf python qt gui编程

2023-06-10 23:50| 来源: 网络整理| 查看: 265

       QMainWindow类提供了一个应用程序窗口。你用它可以让应用程序添加状态栏,工具栏和菜单栏。

1.状态栏。

       状态栏用于显示状态信息。例如:

#!/usr/bin/python #coding:utf-8 import sys from PyQt5.QtWidgets import QMainWindow,QApplication class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): #statusBar()方法创建一个状态栏 #showMessage()状态栏上显示一条消息 self.statusBar().showMessage('这是一个状态提示!') self.setGeometry(300,300,250,150) self.setWindowTitle('Statusbar') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())

运行结果:

                                                 

python gui QT开发pdf python qt gui编程_菜单栏

2.菜单栏。

       菜单栏实际是一种树型结构,为软件的大多数功能提供功能入口。点击以后,即可显示出菜单项。例如:

#!/usr/bin/python #coding:utf-8 import sys from PyQt5.QtWidgets import QMainWindow,QAction,qApp,QApplication from PyQt5.QtGui import QIcon class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): #创建退出特定图标和退出标签 exitAction = QAction(QIcon('2.jpg'), '&Exit', self) #创建退出快捷键 exitAction.setShortcut('Ctrl+Q') #创建退出提示语 exitAction.setStatusTip('Exit application') #点击菜单的时候调用qApp.quit,终止应用程序。 exitAction.triggered.connect(qApp.quit) self.statusBar() #创建一个菜单栏 menubar = self.menuBar() #添加菜单 fileMenu = menubar.addMenu('&File') #添加事件 fileMenu.addAction(exitAction) self.setGeometry(300,300,300,300) self.setWindowTitle('Menubar') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())

       上面代码实现一个简单的具有退出功能的菜单栏,并且添加了快捷方式退出程序。运行结果:

                                                 

python gui QT开发pdf python qt gui编程_python_02

3.工具栏。

       在一个软件程序中,综合各种工具,让用户方便使用的一个区域。例如:

#!/usr/bin/python #coding:utf-8 import sys from PyQt5.QtWidgets import QMainWindow,QAction,qApp,QApplication from PyQt5.QtGui import QIcon class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): exitACtion = QAction(QIcon('2.jpg'),'Exit',self) exitACtion.setShortcut('Ctrl+Q') exitACtion.triggered.connect(qApp.quit) #添加工具栏并添加事件 self.toolbar = self.addToolBar('Exit') self.toolbar.addAction(exitACtion) self.setGeometry(300,300,300,200) self.setWindowTitle('Toolbar') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())

       类似于上面的菜单栏的例子,我们创建一个QAction事件。该事件有一个标签、图标和快捷键。退出窗口的方法,点击绿色图标可以退出。运行结果:

                                                 

python gui QT开发pdf python qt gui编程_GUI编程_03

4.结合菜单栏和工具栏。

代码:

#!/usr/bin/python #coding:utf-8 import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import QIcon class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): #创建文本输入框并把他设置为窗口的布局 textEdit = QTextEdit() self.setCentralWidget(textEdit) exitACtion = QAction(QIcon('2.jpg'),'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) self.toolbar = self.addToolBar('Exit') self.toolbar.addAction(exitACtion) self.setGeometry(300,300,350,250) self.setWindowTitle('Main window') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())

结果:

                                                 

python gui QT开发pdf python qt gui编程_PyQt5_04



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3