Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Object oriented PyQt5 in Python not working

Object oriented PyQt5 in Python not working

Scheduled Pinned Locked Moved Unsolved Qt for Python
9 Posts 3 Posters 803 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Samuel Bachorik
    wrote on 22 Jan 2021, 15:08 last edited by
    #1

    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)
    
    
    J 1 Reply Last reply 22 Jan 2021, 15:45
    0
    • S Samuel Bachorik
      22 Jan 2021, 15:08

      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)
      
      
      J Online
      J Online
      JonB
      wrote on 22 Jan 2021, 15:45 last edited by
      #2

      @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.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 22 Jan 2021, 18:41 last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • S Offline
          S Offline
          Samuel Bachorik
          wrote on 22 Jan 2021, 20:50 last edited by
          #4

          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)
          
          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 22 Jan 2021, 20:56 last edited by
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • S Offline
              S Offline
              Samuel Bachorik
              wrote on 22 Jan 2021, 21:31 last edited by
              #6

              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 ?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 22 Jan 2021, 22:04 last edited by
                #7

                What is the exact goal of that clicked0 method ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Samuel Bachorik
                  wrote on 23 Jan 2021, 07:57 last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 23 Jan 2021, 15:25 last edited by
                    #9

                    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.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1

                    1/9

                    22 Jan 2021, 15:08

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved