PyQt中弹出对话框操作
经常有初学者搞不清楚如何在PyQt中弹出对话框,以及如何处理返回值。这篇文章会举例说明,界面采用手工编写。
我们一般说的对话框指的是模态对话框(Modal Dialogue Box),一旦弹出,就不能对话框以外的窗口进行操作,必须先关闭对话框。
在PyQt中我们一般从QDialog继承创建一个类来操作,根据exec_()方法的返回值判断用户是【确定】还是【取消】了,当然也可以其他返回值,具体看文档。
这个例子创建一个主窗口,有一个表格,记录用户姓名和年龄,一个【添加】按钮,点击弹出对话框,用户输入姓名和年龄,点击【确定】返回,在主窗体表格中插入一行数据。
创建主窗体
为了方便起见使用QWdiget创建主窗体,当然你可以使用QMainWindow,用QTableView和QStandardItemModel来创建表格。
class MainWindow(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
# 创建table和model
table = QtGui.QTableView(parent=self)
self.model = QtGui.QStandardItemModel(parent=self)
self.model.setHorizontalHeaderLabels((u'姓名', u'年龄'))
table.setModel(self.model)
# 创建添加按钮
button = QtGui.QPushButton(u'添加', parent=self)
# 添加信号槽
button.clicked.connect(self.add)
# 创建一个垂直布局,用于防止表格和按钮
layout = QtGui.QVBoxLayout()
layout.addWidget(table)
layout.addWidget(button)
self.setLayout(layout)
def add(self):
pass
创建对话框
对话框从QDialog继承,按钮这里使用QButtonBox来创建,用QButtonBox的好处是创建方便,定义参数即可,并且会自动根据不同平台显示按钮的位置,和各平台风格保持一致,当然默认是英文的,你可以通过国际化来做成中文的。
这里没有做对话框内容的验证,你可以覆盖QDialog的accept方法来进行验证。
下面是对话框的创建代码,为了方便获取姓名和年龄变量,我写了两个方法供外部调用。
class Dialog(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.resize(240, 200)
# 表格布局,用来布局QLabel和QLineEdit及QSpinBox
grid = QtGui.QGridLayout()
grid.addWidget(QtGui.QLabel(u'姓名', parent=self), 0, 0, 1, 1)
self.leName = QtGui.QLineEdit(parent=self)
grid.addWidget(self.leName, 0, 1, 1, 1)
grid.addWidget(QtGui.QLabel(u'年龄', parent=self), 1, 0, 1, 1)
self.sbAge = QtGui.QSpinBox(parent=self)
grid.addWidget(self.sbAge, 1, 1, 1, 1)
# 创建ButtonBox,用户确定和取消
buttonBox = QtGui.QDialogButtonBox(parent=self)
buttonBox.setOrientation(QtCore.Qt.Horizontal) # 设置为水平方向
buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) # 确定和取消两个按钮
# 连接信号和槽
buttonBox.accepted.connect(self.accept) # 确定
buttonBox.rejected.connect(self.reject) # 取消
# 垂直布局,布局表格及按钮
layout = QtGui.QVBoxLayout()
# 加入前面创建的表格布局
layout.addLayout(grid)
# 放一个间隔对象美化布局
spacerItem = QtGui.QSpacerItem(20, 48, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
layout.addItem(spacerItem)
# ButtonBox
layout.addWidget(buttonBox)
self.setLayout(layout)
def name(self):
return self.leName.text()
def age(self):
return self.sbAge.value()
编写对话框调用代码
调用对话框只要使用exec_方法即可,它会弹出对话框并根据用户操作返回值,根据返回值判断是【确定】还是【取消】。
dialog = Dialog(parent=self)
if dialog.exec_():
self.model.appendRow((
QtGui.QStandardItem(dialog.name()),
QtGui.QStandardItem(str(dialog.age())),
))
dialog.destroy()
完整代码和截图
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
class MainWindow(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
# 创建table和model
table = QtGui.QTableView(parent=self)
self.model = QtGui.QStandardItemModel(parent=self)
self.model.setHorizontalHeaderLabels((u'姓名', u'年龄'))
table.setModel(self.model)
# 创建添加按钮
button = QtGui.QPushButton(u'添加', parent=self)
# 添加信号槽
button.clicked.connect(self.add)
# 创建一个垂直布局,用于防止表格和按钮
layout = QtGui.QVBoxLayout()
layout.addWidget(table)
layout.addWidget(button)
self.setLayout(layout)
def add(self):
dialog = Dialog(parent=self)
if dialog.exec_():
self.model.appendRow((
QtGui.QStandardItem(dialog.name()),
QtGui.QStandardItem(str(dialog.age())),
))
dialog.destroy()
class Dialog(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.resize(240, 200)
# 表格布局,用来布局QLabel和QLineEdit及QSpinBox
grid = QtGui.QGridLayout()
grid.addWidget(QtGui.QLabel(u'姓名', parent=self), 0, 0, 1, 1)
self.leName = QtGui.QLineEdit(parent=self)
grid.addWidget(self.leName, 0, 1, 1, 1)
grid.addWidget(QtGui.QLabel(u'年龄', parent=self), 1, 0, 1, 1)
self.sbAge = QtGui.QSpinBox(parent=self)
grid.addWidget(self.sbAge, 1, 1, 1, 1)
# 创建ButtonBox,用户确定和取消
buttonBox = QtGui.QDialogButtonBox(parent=self)
buttonBox.setOrientation(QtCore.Qt.Horizontal) # 设置为水平方向
buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) # 确定和取消两个按钮
# 连接信号和槽
buttonBox.accepted.connect(self.accept) # 确定
buttonBox.rejected.connect(self.reject) # 取消
# 垂直布局,布局表格及按钮
layout = QtGui.QVBoxLayout()
# 加入前面创建的表格布局
layout.addLayout(grid)
# 放一个间隔对象美化布局
spacerItem = QtGui.QSpacerItem(20, 48, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
layout.addItem(spacerItem)
# ButtonBox
layout.addWidget(buttonBox)
self.setLayout(layout)
def name(self):
return self.leName.text()
def age(self):
return self.sbAge.value()
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
PyQt中弹出对话框操作的更多相关文章
- Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解
Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解 在swing中,基于业务的考量,会有对话框来限制用户的行为及对用户的动作进行提示. Swing中 ...
- Android中弹出对话框,AlertDialog关键代码
写在这里便于以后查看. Android中弹出对话框的关键代码: btn01.setOnClickListener(new OnClickListener() { @Override public vo ...
- 在动态链接库dll中弹出对话框
在动态链接库dll中弹出对话框步骤: 1.添加Dialog资源,然后在资源视图的对话框界面右击添加类,输入类名MyDlg,使得其继承与CDialogEx.(继承CDialog应该也可以)2.在新生成的 ...
- 在MFC主对话框OnInitDialog()中弹出对话框
BOOL CXXXDlg::OnInitDialog(){ CDialogEx::OnInitDialog(); SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FA ...
- Android 在Service中弹出对话框
1.在Androidmanifest.xml中插入 <uses-permission android:name="android.permission.SYSTEM_ALERT_WIN ...
- 引用dll动态库,动态库中弹出对话框输入,将输入参数,作为变量继续调用。
在做支付项目时,引用动态库,动态库弹出支付宝或者微信的支付码,继而接收.最终将结果返回给调用动态库方法. 首先,动态库接收的是一个string 类型的xml,如 public string Pay(s ...
- Swing中弹出对话框的几种方式(转)
http://www.cnblogs.com/mailingfeng/archive/2011/12/28/2304289.html 在swing中,基于业务的考量,会有对话框来限制用户的行为及对用户 ...
- jeecg3.7中弹出窗操作标签dgOpenOpt的用法
1.基本参数 参数名 描述 url 弹出页面地址 title ...
- Java中弹出对话框中的几种方式
1.显示一个错误对话框,该对话框显示的 message 为 'alert': JOptionPane.showMessageDialog(null, "alert", " ...
随机推荐
- POJ 1151Atlantis 扫描线+线段树求矩形面积并
题目链接 #include <iostream> #include <vector> #include <cstdio> #include <cstring& ...
- oracle如何修改字段类型(oracle总体知识2)
在一次做开发的时候,遇到需要将数据表的字段类型由number改成varchar,可是该字段又有值, 用 alter table t-name modify cname newType;会报错. 话说 ...
- Hbuilder 常用快捷键汇总
朋友推荐用Hbuilder编辑器,看了下Hbuilder官网和那视频,感觉牛逼哄哄的, 自己也就体验了一下,打开Hbuilder的快捷键列表,每个快捷键都体验了一下,以下展示出来的,每一个都是精华,每 ...
- Noip2010提高组总结
将Noip2010重新做了一遍,第一遍做下来居然只有290分,比当年浙江的一等线低了20分,因为各种坏习惯丢掉了许多分数,Noip时需要特别注意! T1:机器翻译 第一题直接暴力,内存足够所以不用循环 ...
- 利用相关的Aware接口
Struts 2提供了Aware接口.Aware为"感知"的意思,实现了相关Aware接口的Action能够感知相应的资源.Struts在实例化一个Action实例时,如果发现它实 ...
- 外网訪问内网应用实现之无公网IP、多port、固定port、UDP等应用的实现方法
有公网IP时,能够通过路由映射来实现外网訪问内网.然,当没有公网IP时,怎样实现外网訪问内网应用? 硬件路由方法因为无公网不可行,能够使用软件port映射的方法.如开放的NAT123全port映射. ...
- Android Blur效果之FastBlur
Blur 自从iOS系统引入了Blur效果,也就是所谓的毛玻璃.模糊化效果,磨砂效果,各大系统就開始竞相模仿,这是一个怎样的效果呢,我们现来看一些图: 这些就是典型的Blur效果,在iOS和MIUI中 ...
- STL之priority_queue为复合结构排序
priority_queue为复合结构排序: #include <iostream> #include <queue> using namespace std; struct ...
- Register/unregister a dll to GAC
gacutil /i "C:\Test.dll"gacutil /u "Test"
- 字符串分割函数Demo
#include <stdio.h> int getLength(char *string); int main(int argc, char **argv){ char str[12] ...