Important: Please read the Qt Code of Conduct -

Move button functions outside

  • 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 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):
           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.move(68, 219)
           self.b1.resize(60, 35)
       def click(self):      <----- THIS FUNCTION OUTSIDE THIS FILE
            self.texbox.setText("HELLO WORLD")
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = Window()

  • Lifetime Qt Champion


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

  • @Samuel-Bachorik said in Move button functions outside

    class Window(QMainWindow):
       def kokot(self):

    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.

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

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

  • Lifetime Qt Champion

    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.

  • @Samuel-Bachorik , @SGaist
    Actually that was not the @SGaist post I was thinking of. It was 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__().]

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

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

Log in to reply