Important: Please read the 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 ?

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

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

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

  • @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 = ""
    # ...

    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


    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.

    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 functions of all buttons and these functios will just call to its own file with num parameter ,so for examle and in this file would be this code.

    from kkts import abstract
    class Button(abstract):
        def __init__(self,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 so for examlpe my clicked0 wich is conected to b10. I want to put this function outside 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 , For example 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