准备
一个mainwindow和一个widgetform,总代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# -*- 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 父窗体—子窗体
1
2
3
4
5
|
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 子窗体—父窗体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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函数的内容,函数将在子窗体确认按钮点击后执行
1
2
|
def getline( self ): self .textedit.settext( self .dlg.textedit.toplaintext()) |
在子窗体中点击ok,会将子窗体文本框文字传递到父窗体的文本框中
以上这篇pyqt5 实现窗体之间传值的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/color2004/article/details/80021357