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

yipeiwu_com6年前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设计】。

相关文章

django 基于中间件实现限制ip频繁访问过程详解

额额,标题已经很醒目了,通过中间件去实现,其他方法也可以实现 浏览器前端传来的请求,必须通过中间件,才能到后面路由,视图函数,所以我们在中间件那里做一层处理,我们还需要知道是哪个ip,在...

python缩进区别分析

仔细观察下面两个python程序,代码一模一样,但是运行的结果却不同,就是因为最后一行return缩进的不同复制代码 代码如下:def powersum(power, *args): '...

Python设计模式之门面模式简单示例

Python设计模式之门面模式简单示例

本文实例讲述了Python设计模式之门面模式。分享给大家供大家参考,具体如下: facade:建筑物的表面 门面模式是一个软件工程设计模式,主要用于面向对象编程。 一个门面可以看作是为大...

python连接sql server乱码的解决方法

vi /etc/freetds/freetds.conf 复制代码 代码如下:[global]# TDS protocol versiontds version = 8.0client...

200 行python 代码实现 2048 游戏

200 行python 代码实现 2048 游戏

创建游戏文件 2048.py 首先导入需要的包: import curses from random import randrange, choice from collection...