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

yipeiwu_com7年前Python基础

准备

一个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_()

运行点击 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())

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

以上这篇pyQT5 实现窗体之间传值的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

使用py2exe在Windows下将Python程序转为exe文件

前提条件: 需要安装easy-install模块,这是一个python的模块打包工具。 首先下载easy_setup.py的源代码,下载地址: http://pypi.python.o...

Python实现的圆形绘制(画圆)示例

Python实现的圆形绘制(画圆)示例

本文实例讲述了Python实现的圆形绘制。分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- #! python3 import numpy as np...

python在openstreetmap地图上绘制路线图的实现

python在openstreetmap地图上绘制路线图的实现

利用python进行经纬度轨迹展示 嘿!各位好久不见,距离第一次发博客已经过去两年多了,本人也从本科生变成了研究生,好了书归正传,最近在做一个关于航班滑行路径轨迹的项目,目的是将航班的经...

Python中支持向量机SVM的使用方法详解

Python中支持向量机SVM的使用方法详解

除了在Matlab中使用PRTools工具箱中的svm算法,Python中一样可以使用支持向量机做分类。因为Python中的sklearn库也集成了SVM算法,本文的运行环境是Pycha...

python中seaborn包常用图形使用详解

python中seaborn包常用图形使用详解

seaborn包是对matplotlib的增强版,需要安装matplotlib后才能使用。 所有图形都用plt.show()来显示出来,也可以使用下面的创建画布 fig,ax=plt...