pyQT5 实现窗体之间传值的示例


Posted in Python onJune 20, 2019

准备

一个MainWindow和一个WidgetForm,总代码如下

# -*- coding: utf-8 -*-
 
from PyQt5 import QtWidgets
from main_windows import Ui_MainWindow
import sys
from wid_defs import my_widgets
from dlg_defs import my_Dialog
 
class MyWindow(QtWidgets.QMainWindow,Ui_MainWindow):
  def __init__(self):
    super(MyWindow,self).__init__()
    self.setupUi(self)
    
  def openDialog(self):
     self.dlg = my_Dialog()
     www = self.textEdit.toPlainText()
     self.dlg.setT(www)
     self.dlg.exec_()  
    
  def openWidget(self):
    self.wid = my_widgets()
    self.wid.pushButton.clicked.connect(self.GetText)
    www= self.textEdit.toPlainText()
    self.wid.setT(www)    
    self.wid.show() #close wid form
    
    
  def GetText(self):
    self.textEdit.setText(self.wid.textEdit.toPlainText())   
    self.wid.close() 
    
if __name__ == "__main__":
  app = QtWidgets.QApplication(sys.argv)
  mainWindow = MyWindow()
  mainWindow.show()
  sys.exit(app.exec_())

1 父窗体—子窗体

def slot3(self):
     self.dlg = my_Dialog()
     www = self.textEdit.toPlainText()
     self.dlg.setT(www)
     self.dlg.exec_()

1 实例化子窗体:

self.dlg = my_Dialog()

2 直接将父窗体中的变量:

www = self.textEdit.toPlainText()

3 赋给子窗体的对象:

self.dlg.setT(www)

4 再调出子窗体

self.dlg.exec_()

pyQT5 实现窗体之间传值的示例

运行点击 openDialog按钮,会将父窗体textEdit中的内容传到子窗体中。

2 子窗体—父窗体

def slot2(self):
    #widgetForm
    self.wid = my_widgets()
    self.wid.pushButton.clicked.connect(self.GetLine)
    
    #dialog
    self.dlg = my_Dialog()
    self.dlg.buttonBox.accepted.connect(self.GetLine)
    
    www= self.textEdit.toPlainText()
    self.wid.setT(www)    
    self.wid.show()
 
  def GetText(self):
    self.textEdit.setText(self.wid.textEdit.toPlainText())

1 实例化子窗体

self.wid = my_widgets()

2 子窗体按钮(通常是确认按钮)添加关联到父窗体的函数Getline()

(1)widgetForm的方法

self.wid.pushButton.clicked.connect(self.GetLine)

(2)Dialog的方法

self.dlg.buttonBox.accepted.connect(self.GetLine)

3 定义getline函数的内容,函数将在子窗体确认按钮点击后执行

def GetLine(self):
    self.textEdit.setText(self.dlg.textEdit.toPlainText())

pyQT5 实现窗体之间传值的示例

在子窗体中点击OK,会将子窗体文本框文字传递到父窗体的文本框中

以上这篇pyQT5 实现窗体之间传值的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python使用opencv读取图片的实例
Aug 17 Python
详解 Python 与文件对象共事的实例
Sep 11 Python
Python 3实战爬虫之爬取京东图书的图片详解
Oct 09 Python
python实现简易版计算器
Jun 22 Python
django用户登录和注销的实现方法
Jul 16 Python
python自制包并用pip免提交到pypi仅安装到本机【推荐】
Jun 03 Python
python selenium循环登陆网站的实现
Nov 04 Python
TensorFlow设置日志级别的几种方式小结
Feb 04 Python
tensorflow 自定义损失函数示例代码
Feb 05 Python
浅析matlab中imadjust函数
Feb 27 Python
如何快速理解python的垃圾回收机制
Sep 01 Python
手把手教你配置JupyterLab 环境的实现
Feb 02 Python
python3.6环境安装+pip环境配置教程图文详解
Jun 20 #Python
Python 3.6 -win64环境安装PIL模块的教程
Jun 20 #Python
详解Python 调用C# dll库最简方法
Jun 20 #Python
python async with和async for的使用
Jun 20 #Python
python aiohttp的使用详解
Jun 20 #Python
Python 中Django验证码功能的实现代码
Jun 20 #Python
Puppeteer使用示例详解
Jun 20 #Python
You might like
用php获取本周,上周,本月,上月,本季度日期的代码
2009/08/05 PHP
用php随机生成福彩双色球号码的2种方法
2013/02/04 PHP
PHP stripos()函数及注意事项的分析
2013/06/08 PHP
php简单smarty入门程序实例
2015/06/11 PHP
php实现学生管理系统
2020/03/21 PHP
PHP中explode函数和split函数的区别小结
2016/08/24 PHP
Laravel中validation验证 返回中文提示 全局设置的方法
2019/09/29 PHP
实例化php类时传参的方法分析
2020/06/05 PHP
JavaScript类和继承 prototype属性
2010/09/03 Javascript
javascript 实现 秒杀,团购 倒计时展示的记录 分享
2013/07/12 Javascript
js获取html文件的思路及示例
2013/09/17 Javascript
js判断某个方法是否存在实例代码
2015/01/10 Javascript
详解Node.js模块间共享数据库连接的方法
2016/05/24 Javascript
JavaScript代码里的判断小结
2016/08/22 Javascript
详解如何用webpack打包一个网站应用项目
2017/07/12 Javascript
vue axios同步请求解决方案
2017/09/29 Javascript
超出JavaScript安全整数限制的数字计算BigInt详解
2018/06/24 Javascript
使用easyui从servlet传递json数据到前端页面的两种方法
2019/09/05 Javascript
[05:14]辉夜杯主赛事第二日 RECAP精彩回顾
2015/12/27 DOTA
Python3.0与2.X版本的区别实例分析
2014/08/25 Python
python 使用值来排序一个字典的方法
2018/11/16 Python
PyCharm鼠标右键不显示Run unittest的解决方法
2018/11/30 Python
python basemap 画出经纬度并标定的实例
2019/07/09 Python
对python中url参数编码与解码的实例详解
2019/07/25 Python
python创建子类的方法分析
2019/11/28 Python
python接口自动化框架实战
2020/12/23 Python
中国跨境在线时尚零售商:Bellelily
2018/04/06 全球购物
全球异乡人的跨境社交电商平台:Kouhigh口嗨网
2020/07/24 全球购物
网络事业创业计划书范文
2014/01/09 职场文书
高三励志标语
2014/06/05 职场文书
公司采购主管岗位职责
2014/06/17 职场文书
小学生读书笔记
2015/07/01 职场文书
演讲比赛通讯稿
2015/07/18 职场文书
2016入党积极分子党课学习心得体会
2015/10/09 职场文书
私人贷款担保书该怎么写呢?
2019/07/02 职场文书
使用HTML+Css+transform实现3D导航栏的示例代码
2021/03/31 HTML / CSS