如何使用另一个py脚本在pyqt中的文本框中获取输入

时间:2019-08-27 07:48:20

标签: python pyqt5

我有这两个脚本。首先包含使用pyqt5的ui,其中包含一些文本框和按钮。第二个包含按钮的功能(我是说它会做什么)。现在,我只想在终端中显示文本框中的内容。问题是我无法使用第二个py获取/打印/显示文本框中的输入文本。我有这个按钮(上载按钮),单击时必须在终端中显示此文本。

我尝试将第二个py导入到第一个。然后我连接按钮。        butUpld.clicked.connect(py2.up)

对于第二个py,我也将其连接到第一个

This is the first py file name py1.py

import os, glob, sys, time
import py2
from PyQt5.Qt import QApplication, QClipboard, QUrl
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QMainWindow, QWidget, QPlainTextEdit,   QLabel, QGridLayout, QPushButton, QLineEdit, QTextEdit, QMessageBox, QComboBox, QTabWidget, QFormLayout, QHBoxLayout, QRadioButton, QCheckBox, QVBoxLayout, QFileDialog, QButtonGroup
from PyQt5.QtCore import pyqtSlot, Qt

qt_app = QApplication(sys.argv)

class Layoutexe(QTabWidget):
    def __init__(self, parent = None):
        super(Layoutexe, self).__init__(parent)
        self.tab1 = QWidget()
        self.tab2 = QWidget()

        self.addTab(self.tab1," ")
        self.addTab(self.tab2," ")
        self.tab1UI()
        self.tab2UI()
        self.setWindowTitle(" ")
        self.setFixedSize(900,1000)

    def tab1UI(self):
        vbox = QVBoxLayout()
        hbox = QHBoxLayout()
        hbox1 = QHBoxLayout()
        hbox2 = QHBoxLayout()
        hbox3 = QHBoxLayout()

        labelPath = QLabel("PATH:")
        tboxPath = QLineEdit(self)
        butSrch = QPushButton("Search", self)
        butClr = QPushButton("Clear", self)
        hbox.addStretch()
        hbox.addWidget(labelPath)
        hbox.addWidget(tboxPath)
        hbox.addWidget(butSrch)
        hbox.addStretch()
        hbox.addWidget(butClr)
        vbox.addLayout(hbox)

        radbutLst = QRadioButton("Latest")
        radbutMnl = QRadioButton("Manual")
        hbox1.addWidget(radbutLst)
        hbox1.addWidget(radbutMnl)
        hbox1.addStretch()
        vbox.addLayout(hbox1)

        labelEV = QLabel("Event:")
        tboxEV = QLineEdit(self)
        hbox2.addWidget(labelEV)
        hbox2.addWidget(tboxEV)
        hbox2.addStretch()
        vbox.addLayout(hbox2)

        butUpld = QPushButton("UPLOAD", self)
        hbox3.addStretch()
        hbox3.addWidget(butUpld)
        hbox3.addStretch()
        vbox.addLayout(hbox3)

        vbox.addStretch()
        self.tab1.setLayout(vbox)

    butUpld.clicked.connect(py2.up)

    def tab2UI(self):
        vbox = QVBoxLayout()
        layout = QHBoxLayout()

    def run(self):
        self.show()
        qt_app.exec_()

app = Layoutexe()
app.run()



this the second py file named py2.py

import os, glob
import py1

def up():
    a = gui.rep.tab1U.tboxEV
    print(a.text())

1 个答案:

答案 0 :(得分:0)

尝试一下:

py1.py

import sys
#import os, glob, time

from PyQt5.Qt import QApplication, QClipboard, QUrl
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QMainWindow, QWidget, QPlainTextEdit,   QLabel, QGridLayout, QPushButton, QLineEdit, QTextEdit, QMessageBox, QComboBox, QTabWidget, QFormLayout, QHBoxLayout, QRadioButton, QCheckBox, QVBoxLayout, QFileDialog, QButtonGroup
from PyQt5.QtCore import pyqtSlot, Qt

import py2         

qt_app = QApplication(sys.argv)

class Layoutexe(QTabWidget):
    def __init__(self, parent = None):
        super(Layoutexe, self).__init__(parent)

        self.tab1 = QWidget()
        self.tab2 = QWidget()

        self.addTab(self.tab1," ")
        self.addTab(self.tab2," ")
        self.tab1UI()
        self.tab2UI()
        self.setWindowTitle(" ")
        self.setFixedSize(900,1000)

    def tab1UI(self):
        vbox = QVBoxLayout()
        hbox = QHBoxLayout()
        hbox1 = QHBoxLayout()
        hbox2 = QHBoxLayout()
        hbox3 = QHBoxLayout()

        labelPath = QLabel("PATH:")
        tboxPath = QLineEdit(self)
        butSrch = QPushButton("Search", self)
        butClr = QPushButton("Clear", self)
        hbox.addStretch()
        hbox.addWidget(labelPath)
        hbox.addWidget(tboxPath)
        hbox.addWidget(butSrch)
        hbox.addStretch()
        hbox.addWidget(butClr)
        vbox.addLayout(hbox)

        radbutLst = QRadioButton("Latest")
        radbutMnl = QRadioButton("Manual")
        hbox1.addWidget(radbutLst)
        hbox1.addWidget(radbutMnl)
        hbox1.addStretch()
        vbox.addLayout(hbox1)

        labelEV = QLabel("Event:")
        self.tboxEV = QLineEdit(self)
        hbox2.addWidget(labelEV)
        hbox2.addWidget(self.tboxEV)
        hbox2.addStretch()
        vbox.addLayout(hbox2)

        butUpld = QPushButton("UPLOAD", self)
        hbox3.addStretch()
        hbox3.addWidget(butUpld)
        hbox3.addStretch()
        vbox.addLayout(hbox3)

        vbox.addStretch()
        self.tab1.setLayout(vbox)

        butUpld.clicked.connect(lambda _, s=self: py2.up(s))             # +++

    def tab2UI(self):
        vbox = QVBoxLayout()
        layout = QHBoxLayout()

    def run(self):
        self.show()
        qt_app.exec_()

app = Layoutexe()
app.run()

py2.py

#import os, glob
#import py1

def up(self):
#    a = gui.rep.tab1U.tboxEV
    a = self.tboxEV
    print(a.text())

enter image description here