-2

Assume we have an interface with Buttons and LineEdits in one .py file. I have this code in another, which inherits it:

import Inter_Input_Blasting as interf
from PyQt6 import QtCore,QtWidgets,QtGui
from functools import partial

class MainWindow(QtWidgets.QMainWindow):

def on_clicked(self):
    print("Button Pushed")

def __init__(self,parent=None):
    super(MainWindow, self).__init__(parent)
    self.ui = interf.Ui_MainWindow()
    self.ui.setupUi(self)
    self.ui.calc_button.clicked.connect(MainWindow.on_clicked)

    self.ui.input_overall_1.textChanged.connect(MainWindow.gather_data)


def gather_data(self):
    return self.ui.input_overall_1.text()


if __name__== "__main__":
    import sys
    app = interf.QtWidgets.QApplication(sys.argv)
    Form = MainWindow()
    Form.show()
    sys.exit(app.exec())

So, i need to print my values into console when i put it in the lineedit fiels. The .textChanged() method is working, but the .gather_data() isn't.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Addm1X
  • 27
  • 7
  • it's not that relevant to print these numbers, but to store in variables to access them later on – Addm1X Sep 14 '21 at 11:25
  • Your question is confusing. First of all, connections to functions that access an instance, *must* be done to the instance, not the class, so it's `self.on_clicked` and `self.gather_data`. Then, you say that you want to print text to console, but then that you want to store those values. Please try to make yourself clear, remember that if you want to add details you can also [edit] your post, and if you don't know why the `self` changed the result, please do some serious research and studying about what classes and instances are, because that difference is *mandatory* knowledge for OOP. – musicamante Sep 14 '21 at 13:47
  • @musicamante i'am sorry for misunderstanding. Actually, i'm doing a research about python and pyqt right now. And those questions is just for better understanding. Also I'm Russian, so it might be the bad English as well – Addm1X Sep 15 '21 at 06:38

1 Answers1

-2

Provide a variable for storing the text:

def __init__(self,parent=None):
    self.txt = None

Then in method gather_data, store the text in that variable:

def gather_data(self):
    sef.txt = self.ui.input_overall_1.text()

then before sys.exit, print that value:

r = app.exec()
print(Form.txt)
sys.exit(r)
fferri
  • 18,285
  • 5
  • 46
  • 95
  • strange, but it says that i don't define the name 'txt' – Addm1X Sep 14 '21 at 11:16
  • ok, i just changed MainWindow to self and it's working. but print can be putted inside the .gather_data() method as well – Addm1X Sep 14 '21 at 13:29
  • 1
    While this solution "works", it's doesn't answer very well to the problem: 1. Using globals for this is pointless; 2. The problem was about the usage of instance methods as they where class functions. – musicamante Sep 14 '21 at 13:42