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. Move button functions outside main.py
QtWS25 Last Chance

Move button functions outside main.py

Scheduled Pinned Locked Moved Unsolved Qt for Python
8 Posts 3 Posters 1.2k 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.
  • Samuel BachorikS Offline
    Samuel BachorikS Offline
    Samuel Bachorik
    wrote on last edited by Samuel Bachorik
    #1

    Hello, iam wondering if is possible to make in PyQt5 file for design of my app and file for functionality of my app. By this i mean for example buttons, every button in code is connected to some funtion. I want to store these functions outside file where is defined button. Issomething like this possible ?

    Example code - function click is in same file where button is created, how can i move this function in another file and make it work ? So by this i mean for example create Buttons.py file and store all functions of buttons there

    from PyQt5 import QtWidgets
    import sys
    from PyQt5.QtWidgets import QMainWindow, QApplication, QLineEdit
    class Window(QMainWindow):
       def kokot(self):
           super().__init__()
           self.setWindowTitle("Samuel")
           self.setGeometry(200, 200, 259, 258)
    
           self.textbox = QLineEdit(self)
           self.textbox.move(5, 5)
           self.textbox.resize(188, 35)
           self.textboxValue = self.textbox.text()
    
           self.b1 = QtWidgets.QPushButton(self)
           self.b1.setText("0")
           self.b1.move(68, 219)
           self.b1.resize(60, 35)
           self.b1.clicked.connect(self.click)
           self.show()
    
       def click(self):      <----- THIS FUNCTION OUTSIDE THIS FILE
            self.texbox.setText("HELLO WORLD")
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = Window()
        window.kokot()
        sys.exit(app.exec())
    
    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Since you are manipulating your Window internal state in the click method, you should rather move the whole class outside that file.

      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
      • Samuel BachorikS Samuel Bachorik

        Hello, iam wondering if is possible to make in PyQt5 file for design of my app and file for functionality of my app. By this i mean for example buttons, every button in code is connected to some funtion. I want to store these functions outside file where is defined button. Issomething like this possible ?

        Example code - function click is in same file where button is created, how can i move this function in another file and make it work ? So by this i mean for example create Buttons.py file and store all functions of buttons there

        from PyQt5 import QtWidgets
        import sys
        from PyQt5.QtWidgets import QMainWindow, QApplication, QLineEdit
        class Window(QMainWindow):
           def kokot(self):
               super().__init__()
               self.setWindowTitle("Samuel")
               self.setGeometry(200, 200, 259, 258)
        
               self.textbox = QLineEdit(self)
               self.textbox.move(5, 5)
               self.textbox.resize(188, 35)
               self.textboxValue = self.textbox.text()
        
               self.b1 = QtWidgets.QPushButton(self)
               self.b1.setText("0")
               self.b1.move(68, 219)
               self.b1.resize(60, 35)
               self.b1.clicked.connect(self.click)
               self.show()
        
           def click(self):      <----- THIS FUNCTION OUTSIDE THIS FILE
                self.texbox.setText("HELLO WORLD")
        
        if __name__ == "__main__":
            app = QApplication(sys.argv)
            window = Window()
            window.kokot()
            sys.exit(app.exec())
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Samuel-Bachorik said in Move button functions outside main.py:

        class Window(QMainWindow):
           def kokot(self):
               super().__init__()
        

        In addition to @SGaist. In your other thread with similar question, he already told you this is incorrect/bad, and what to do about it. It would be really good if you heeded people's answers and applied what they said rather than ignoring it.

        1 Reply Last reply
        0
        • Samuel BachorikS Offline
          Samuel BachorikS Offline
          Samuel Bachorik
          wrote on last edited by Samuel Bachorik
          #4

          @JonB i always take tips from you but in this question, ↓ iam using __ init __ and he said it is bad that i use constructor. Here in this older question iam using __ init __ and he said it is wrong.... first reply in this question -> https://forum.qt.io/topic/122722/why-click-on-button-opens-new-pyqt-window/2

          So after that question i made from def__ init __ (self): TO def kokot(self):

          JonBJ 2 Replies Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by SGaist
            #5

            It's absolutely not what I said. I told you that it was wrong to call the show method in the construction phase nothing else.

            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
            • Samuel BachorikS Samuel Bachorik

              @JonB i always take tips from you but in this question, ↓ iam using __ init __ and he said it is bad that i use constructor. Here in this older question iam using __ init __ and he said it is wrong.... first reply in this question -> https://forum.qt.io/topic/122722/why-click-on-button-opens-new-pyqt-window/2

              So after that question i made from def__ init __ (self): TO def kokot(self):

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Samuel-Bachorik , @SGaist
              Actually that was not the @SGaist post I was thinking of. It was https://forum.qt.io/topic/122996/object-oriented-pyqt5-in-python-not-working/3 where he wrote:

              First thing: kokot should rather be __init__.

              And indeed that should have been the first thing you corrected. Anyway, I don't mean to be overly hard on you, so: please do so now, you should be calling your super().__init__() from your own __init__(), not from some method named kokot(). [If you really want, you could keep the other self.... initializations you do there in some kokot() you call, though I wouldn't recommend it, but the super().__init__() belongs in an __init__().]

              1 Reply Last reply
              2
              • Samuel BachorikS Samuel Bachorik

                @JonB i always take tips from you but in this question, ↓ iam using __ init __ and he said it is bad that i use constructor. Here in this older question iam using __ init __ and he said it is wrong.... first reply in this question -> https://forum.qt.io/topic/122722/why-click-on-button-opens-new-pyqt-window/2

                So after that question i made from def__ init __ (self): TO def kokot(self):

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Samuel-Bachorik
                Back to your current intention/question:

                   def click(self):      <----- THIS FUNCTION OUTSIDE THIS FILE
                        self.texbox.setText("HELLO WORLD")
                

                Why do you want to achieve that? The code you have in that slot alters something in self, the Window here. The best place for this is indeed inside Window where it is now. If you move it outside this class/module you will have to code some mechanism for it to access the Window.textbox.

                There are cases where this makes sense (perhaps by emitting a signal) but you wanting to do it now seems to me to just introduce compliaction.

                Now, it is a different question if you want that click() slot outside of Window where nothing in the slot code will want to access/affect anything in Window. And we can discuss that if you wish. But that's not the case you (currently) show.

                1 Reply Last reply
                2
                • Samuel BachorikS Offline
                  Samuel BachorikS Offline
                  Samuel Bachorik
                  wrote on last edited by
                  #8

                  @SGaist I am sorry my bad, I misunderstood it, my apologize to you. @JonB Thank for reply, the main reason i want to do that is because i want to have my code more clear, when i look on internet and i see how very object oriented are all aplications i want to do same. My calculator is some bunch of code (which works actualy) where on top i have PyQt design (buttons,labels,plaintext) and under that 30 functions of buttons.... This code is really long after that and i wanted to make it more celar. When i code in Android studio for example, i have for everything its own class. For example ButtonManager, Design have xml. file. Mathematic operations have also its own class and i just call it from main class. But here in Python everything is written in one file and it is very unclear. So this is the reason why i want to do that.

                  Thank you.

                  1 Reply Last reply
                  1

                  • Login

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