PyQt界面控件常用积累 - Oejia 技术栈,企业方案分享、Odoo顾问
Oejia 技术栈
主页
分享
微信模块
索引
关于
订阅
编辑器
登录
PyQt界面控件常用积累
Oejia
on 2014-12-06 13:27:09
###初始化 ```python QtGui.QApplication(sys.argv) #QApplication 类初始化 sys.exit(app.exec_()) #进入消息循环,等待窗体退出 ``` ###创建主界面的两种方法 ```python # 1.通过继承QtGui.QMainWindow创建类 QtGui.QMainWindow.__init__(self) # 调用父类初始化方法 # 2.通过继承QtGui.QWidget创建类 QtGui.QWidget.__init__(self) # 调用父类初始化方法 QPushButton # 按钮 setFlat(True) #设置文件样式按钮 ``` ###连接事件信号的两种方法 ```python # 1.利用主界面self的connect方法 self.connect(self.button1, # Button1事件 QtCore.SIGNAL('clicked()'), # clicked()信号 self.OnButton1 # 插槽函数 ) # 2.利用控件本身connect方法 button.clicked.connect(showdata) ``` ###对话窗体基本用法 ```python # 定义 class MyDialog(QtGui.QDialog): # 继承QtGui.QDialog ... self.done(1) # 结束对话框返回1 # 调用 dialog = MyDialog() # 创建对话框对象 r = dialog.exec_() # 运行对话框 if r: self.button.setText(dialog.text) # 文本标签控件QLabel QtGui.QLabel('PyQt\nLabel') # 创建标签 label1.setAlignment(QtCore.Qt.AlignCenter) # 居中对齐 # 单行文本框控件QLineEdit edit1 = QtGui.QLineEdit() # 创建单行文本框 edit2.setEchoMode(QtGui.QLineEdit.Password) # 将其设置为密码框 # 多行文本控件QTextEdit edit = QtGui.QTextEdit() # 创建多行文本框 edit.setText('Python\nPyQt') # 设置文本框中的文字 # 表格式布局gridlayout gridlayout.setRowMinimumHeight (1, 180) # 设置第二行的最小高度为108 # 窗体菜单栏控件menuBar的基本用法 class MyWindow(QtGui.QMainWindow): # 通过继承QtGui.QMainWindow创建类 menubar = self.menuBar() # 获得窗口的菜单条 file = menubar.addMenu('&File') # 添加File菜单 file.addAction('Open') # 添加Open命令 open = self.file.addAction('Open') # 添加Open命令 self.connect(open, QtCore.SIGNAL('triggered()'), self.OnOpen) # 菜单信号 # 退出主窗体界面 self.close() # 界面右键菜单用法 def contextMenuEvent(self, event): # 重载弹出式菜单事件 self.file.exec_(event.globalPos()) # 常用消息框用法 QtGui.QMessageBox.about(self, 'PyQt', 'About') # 创建About消息框 QtGui.QMessageBox.aboutQt(self, 'PyQt') # 创建AboutQt消息框 # 创建Ctitical消息框 r = QtGui.QMessageBox.critical(self, 'PyQt', 'Critical', QtGui.QMessageBox.Abort, QtGui.QMessageBox.Retry, QtGui.QMessageBox.Ignore ) # 返回状态判断 if r == QtGui.QMessageBox.Abort: self.label.setText('Abort') elif r == QtGui.QMessageBox.Retry: self.label.setText('Retry') else: self.label.setText('Ignore') QtGui.QMessageBox.information(self, 'PyQt', 'Information') # 创建Information消息框 # 创建Question消息框 r = QtGui.QMessageBox.question(self, 'PyQt', 'Question', QtGui.QMessageBox.Yes, QtGui.QMessageBox.No, QtGui.QMessageBox.Cancel ) # 创建Warning消息框 r = QtGui.QMessageBox.warning(self, 'PyQt', 'Warning', QtGui.QMessageBox.Yes, QtGui.QMessageBox.No ) # 单选按钮复选按钮的用法 self.radio1 = QtGui.QRadioButton('Radio1') # 创建单选框 self.radio2 = QtGui.QRadioButton('Radio2') self.check = QtGui.QCheckBox('check') # 创建复选框 self.check.setChecked(True) # 将复选框选中 # 状态获取 if self.radio1.isChecked(): if self.check.isChecked(): # xml界面文件的用法 from PyQt4 import QtCore, QtGui, uic class MyDialog(QtGui.QDialog): # 继承QtGui.QDialog def__init__(self): QtGui.QWidget.__init__(self) uic.loadUi("res.ui", self) def OnButton(self): # 处理按钮事件 dialog = MyDialog() # 创建对话框对象 r = dialog.exec_() # 运行对话框 if r: self.button.setText(dialog.lineEdit.text())#获取对话框中控件元素的值 # 空白项控件QSpacerItem的用法 spacer1 = QtGui.QSpacerItem(300,40) # 创建空白项 gridlayout.addItem(spacer1, 0 ,0) # 添加空白项 ``` ###标准系统对话框用法 ```python filename = QtGui.QFileDialog.getOpenFileName(self, 'Open') #创建文件打开对话框 if not filename.isEmpty(): self.label.setText(filename) font, ok = QtGui.QFontDialog.getFont() # 创建字体选中对话框 if ok: self.label.setText(font.key()) color = QtGui.QColorDialog.getColor() # 创建颜色选择对话框 if color.isValid(): self.label.setText(color.name()) ```
Information
PyQt界面控件常用积累
http://www.oejia.net/blog/2014/12/06/pyqt-ui-wt.html
http://www.oejia.net/raw/2014/12/06/pyqt-ui-wt.md
Oejia
on 2014-12-06 13:27:09
Category
桌面UI
Tag
Python
PyQT
Related
2019-03-25 :
Odoo 基于企业微信实现的通用审批流功能的自助配置及使用说明
2018-09-13 :
Odoo 开源微信小程序商城模块
2018-07-06 :
微信模块 Oejia_wx v0.5.3 发布,增加聊天图片和语音的支持
2018-04-24 :
DjangoX v0.5 发布,支持Python3的强大灵活后台系统
2018-02-08 :
Odoo 中的列表页如何默认就显示出筛选及分组功能菜单
2018-02-08 :
Odoo 如何隐藏超级管理员用户
2017-11-16 :
DjangoX 扩展的命令(commands)使用指南
2016-09-21 :
如何在 Django 中使用 Tornado 的 WebSocket 并做请求的用户验证识别
2016-02-23 :
YouMd 基本使用说明
2016-01-17 :
新版开启2016年新篇章
官方公众号
关注公众号实时咨询我们
分类
Odoo (68)
DjangoX (11)
后端HTTP (7)
YouMd (4)
移动开发 (4)
Django (4)
公告说明 (2)
JS (2)
Python (1)
前端 (1)
Nginx (1)
桌面UI (1)
December 2019
Sun
Mon
Tue
Wed
Thu
Fri
Sat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
About This Entry
系统修复和更新记录
Cordova开发环境搭建与基本使用
About This Blog
main index
to find recently
archives
to find all
标签
微信模块
Python
小程序商城
Web
Http
Cordova
客服系统
微信客服系统
HTML5
微信客服
Markdown
解析
DjangoX
微信模块发布记录
客服工单系统
ExtJS
示例
Excel
JS
新年
PyQT
YouPBX
扩展开发
最近发布
YouPBX 基本使用说明
小程序商城分销模块的使用说明
YouPBX 安装部署基本说明
Odoo 小程序商城模板消息通知使用说明
Odoo 国内发票 OCR 导入功能介绍
微信模块 Oejia_wx v0.5.9 发布,系列视图及对接优化,增加Odoo13支持
小程序商城模块 Oejia_weshop v0.1.5 发布,新版的UI及系列后端优化
Odoo 地图位置改为百度地图
小程序商城模块 Oejia_weshop v0.1.4 发布,支持Odoo账号绑定登录及价格表机制
Odoo 扫码及授权免密登录通用模块使用说明
Friend Links
Oejia技术梦博客分享
YouMd,爱上MarkDown
Mole轻量级wsgi架子