Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Object oriented PyQt5 in Python not working



  • Hello let me show you my code files. Iam trying to set text to QLineEdit by this way. And this is just still not and not working.

    Can someone help ?

    test.py

    from PyQt5 import QtWidgets
    import sys
    from PyQt5.QtWidgets import QMainWindow, QApplication, QLineEdit
    class Window(QMainWindow):
       def kokot(self):
           super().__init__()
           self.textboxvalue2 = ""
           self.setWindowTitle("Samuel")
           self.setGeometry(200, 200, 259, 258)
    
           self.textbox = QLineEdit(self)
           self.textbox.move(5, 5)
           self.textbox.resize(188, 35)
           self.textbox.setText(str(self.textboxvalue2))
           self.textbox.setPlaceholderText("Enter numbers")
           self.textboxValue = self.textbox.text()
    
           self.b10 = QtWidgets.QPushButton(self)
           self.b10.setText("0")
           self.b10.move(68, 219)
           self.b10.resize(60, 35)
           self.b10.clicked.connect(self.clicked0)
           self.show()
    
       def setExpression(self,expression):
           self.textbox.setText(expression)
    
       def clicked0(self):
           import Buttons
           z = Buttons.Button("0")
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = Window()
        window.kokot()
        sys.exit(app.exec())
    

    Buttons.py

    from kkts import abstract
    class Button(abstract):
        def __init__(self,num):
            super().buttonSent(num)
    
    

    kkts.py

    import test
    from abc import ABC, abstractmethod
    x = test.Window()
    class abstract():
        @abstractmethod
        def buttonSent(self,number):
            global x
            x.setExpression(number)
    
    


  • @Samuel-Bachorik
    Your title reads "Object oriented PyQt5 in Python not working", I do not see any connection between that and your question?

    I see statements:

    self.textboxvalue2 = ""
    # ...
    self.textbox.setText(str(self.textboxvalue2))
    

    so that will set textbox (which is a QLineEdit)'s text to "".

    I don't know what you're trying to do in clicked0. You have a local variable z set to a new Buttons.Button, which then immediately goes out of scope and so is destroyed. I have no clue what you are trying to do with your abstract & Button classes. As we have said elsewhere, no idea why you are using a global, what x = test.Window() is about, and so on. None of it looks at all right.

    So... I don't know what is "just still not and not working", nor what you are trying to achieve. And I'd be suprised if anyone else does from your questions and the code. I think you need to formulate some clear objective and explain/ask what your issue is.


  • Lifetime Qt Champion

    Hi,

    First thing: kokot should rather be __init__. You have a global variable that has nothing to do with the instance you create in the __main__ function.

    That code is unclean on several levels. You really should explain what you are trying to do.



  • Hello guys thank for reply, i have made simple calcualtor in PyQt5 long time ago and now iam trying to make it object oriented. Here on my Github is andorid calculator made same way.
    https://github.com/Samuel-Bachorik/Andorid-calculator-with-created-Eval-function/tree/master/app/src/main/java/com/example/myapplication2/buttonManager/buttons

    As you can see here, every button have its own java class and there is java class named AbstractButton. This Abstract button class have Abstract class in it and every button calls this class.

    So in python, i want in test.py functions of all buttons and these functios will just call to its own number.py file with num parameter ,so for examle NumberFive.py and in this file would be this code.

    from kkts import abstract
    class Button(abstract):
        def __init__(self,num):
            super().buttonSent(num)
    

  • Lifetime Qt Champion

    Don't go that way. Python and Java are two different languages. Do not try to reproduce Java idiosyncrasies here. Learn how Python works without trying to find Java again in it.

    For your buttons you already have a base class named QPushButton. If you need them to all have something more, then create a new class based on QPushButton and then use that is your GUI.



  • I just want to store buttons functions outside test.py so for examlpe my clicked0 wich is conected to b10. I want to put this function outside test.py is it possible please ?


  • Lifetime Qt Champion

    What is the exact goal of that clicked0 method ?



  • In this case this method changes text of plaint text, this is just piece of code full have 500 lines... Simply,i want two python files, one for my aplication design and second for functionality. When i connect some button to function i want to move this function outside test.py. , For example buttons.py and store all button functions there.

    Thank you


  • Lifetime Qt Champion

    You are taking the problem on the wrong end.

    From a coding point of view, moving all the code that concerns one specific type of widgets in a different file does not make anything clearer.

    Keep each of your custom widgets in its own file so their code is in one coherent place.


Log in to reply