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

相关文章

使用python加密自己的密码

有些时候我们不得不在自己的代码里写上密码,为了安全起见,我们可以为自己的密码加密 先上段代码,这个代码是转自网上 root@ProFtp:/usr/lib/python2.7# mo...

浅谈插入排序算法在Python程序中的实现及简单改进

Python实现插入排序的一般范例为: #coding=cp936 #coding=cp936 #插入排序算法 def InsertionSort(A): for j in r...

django-crontab 定时执行任务方法的实现

需求 每天请求一封邮件,并读取该邮件 这个其实可以使用linux 自带了crontab实现,但是毕竟是django 开发。想着不知道有没有方法可以从django 中实现。 简单搜索了下...

详解Python中的array数组模块相关使用

初始化 array实例化可以提供一个参数来描述允许那种数据类型,还可以有一个初始的数据序列存储在数组中。 import array import binascii s = 'Thi...

windows下Python安装、使用教程和Notepad++的使用教程

windows下Python安装、使用教程和Notepad++的使用教程

一、Python下载 1.进入Python官网:https://www.python.org/ 2.选择windows版本(Download > Windows) 3.点击下载P...