D
D
dima_love_friends2021-08-24 14:08:55
Python
dima_love_friends, 2021-08-24 14:08:55

How to refer to a variable of another class?

I work with PyQt5 in Python.
There are classes MyWidget(QMainWindow) and ThreadClassCheck(QtCore.QThread).
MyWidget opens an interface with 2 input fields and 1 button. After pressing the button from the first (upper) input field, this word is transferred to the second input field (while this word also remains in the first one).
How can the ThreadClassCheck(QtCore.QThread) class access the value of these two fields from the MyWidget class?
t.ui design:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>491</width>
    <height>276</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>190</y>
      <width>93</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>кнопка</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEditInput">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>40</y>
      <width>321</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>40</y>
      <width>51</width>
      <height>31</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 12pt &quot;MS Shell Dlg 2&quot;;</string>
    </property>
    <property name="text">
     <string>Ввод</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>50</x>
      <y>120</y>
      <width>71</width>
      <height>31</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 12pt &quot;MS Shell Dlg 2&quot;;</string>
    </property>
    <property name="text">
     <string>Вывод</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="lineEdit_2Output">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>120</y>
      <width>321</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>491</width>
     <height>26</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

The code:
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtCore
from PyQt5 import uic

import sys
import time


class ThreadClassCheck(QtCore.QThread):
    signal = QtCore.pyqtSignal(str)

    def __init__(self, checkword, parent=None):
        super(ThreadClassCheck, self).__init__(parent)
        self.is_going = True
        self.checkword = checkword

    def display(self):
        print('ниже выведем первое поле')
        print(self.lineEditInput.text())

    def run(self):
        while True:
            # как здесь получить переменную значение из двух полей ввода?
            # self.text1 = self.lineEditInput.text()
            # self.text2 = self.lineEdit_2Output.text()
            # в интерфейсе, но не в классе ThreadClass

            ex = MyWidget()  # экземпляр интерфейса
            ThreadClassCheck.display(ex)  # такой вызов тоже не работает, пустая строка

            time.sleep(3)
            sms = "check"
            self.signal.emit(sms)

    def stop(self):
        self.is_going = False
        self.terminate()


class MyWidget(QMainWindow):
    def __init__(self):
        super().__init__()
        uic.loadUi('t.ui', self)
        self.pushButton.clicked.connect(self.btn)

        self.text2 = self.lineEdit_2Output.text()
        self.text1 = self.lineEditInput.text()

    def btn(self):
        print('кнопка нажата, данные из первой строки:', self.text1)
        print('кнопка нажата, данные из второй строки:', self.text2)
        self.lineEdit_2Output.setText(self.text1)  # из первого поля ввода переносим во второй


def except_hook(cls, exception, traceback):
    sys.__excepthook__(cls, exception, traceback)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyWidget()
    ex.show()
    sys.excepthook = except_hook
    sys.exit(app.exec_())

Tried through an instance, but for some reason it outputs an empty string

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivanzolo2001, 2021-08-24
@Ivanzolo2001

like you can just create an object of this class without calling any constructors through it, and so on. And through the object to access whatever you want. If it doesn't work, try using "self.variable name".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question