0

I have a file that contains a class definition. This class definition stores Tkinter Button objects, and when these buttons are pressed, I need them to run a function in main.py. My file structure is as follows:

 - main.py
 - Classes (Folder)
     - class.py
     - another_class.py

class.py contains a class with a method render, which creates and shows the button, saving it to an attribute button:

    def Render(self):
        self.__scrnPos = self.GetScrnPos()

        if self.tile == None:
            txt    = self.multiplier
        else:
            txt    = self.tile.GetLetter() + str(self.tile.GetValue())
        self.colour = self.__GetColour()

        button = tk.Button(
            text    = txt,
            font    = self.font,
            command = lambda: test(),
            bg      = self.colour).place(
            x       = self.__scrnPos[0],
            y       = self.__scrnPos[1],
            width   = self.__size,
            height  = self.__size)

        return button

When the button is pressed, it should run the subroutine test in main.py.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lordimass
  • 97
  • 2
  • 6
  • If I have understood what you mean correctly, you want to import a function from the parent directory. right ? – Khanzadeh_AH Jun 26 '23 at 17:49
  • @Khanzadeh_AH Not quite, I need it to run something in main.py, because there are variables and other things stored there. I might be able to work with that though, is it possible? – Lordimass Jun 26 '23 at 19:41
  • It is better to provide a [mre] as currently there is no information how those files are related, for example how and where is the instance of the class defined in `class.py` created? – acw1668 Jun 27 '23 at 00:59
  • @acw1668 `class.py` just contains a class definition, nothing else, I'm trying to keep my code organised by keeping things in separate files so I don't have to scroll through thousands of lines of code later on. – Lordimass Jun 27 '23 at 07:16
  • @Lordimass I am asking how and where the class is created. Without a [mre], it is hard to identify the issue. – acw1668 Jun 27 '23 at 07:22
  • @acw1668 The class is created in class.py, I'm not sure what else to tell you. Literally the only thing in the file *is* the class definition. My problem has been solved by someone else anyway, thanks anyways. – Lordimass Jun 27 '23 at 07:28

1 Answers1

-3

To execute a function in the main.py file from a child file, you can pass a reference to the function as an argument to the child file or define a callback function in the child file and invoke it from the child file's button.

Here's an example of how you can achieve this: main.py

def test():
    print("Button pressed!")

class.py

def Render(self, callback):
    # ...
    button = tk.Button(
        text=txt,
        font=self.font,
        command=callback,  # Set the command to the provided callback function
        bg=self.colour
    )

In main.py, when you instantiate the class and call the Render method, pass the test function as an argument:

obj = MyClass()
obj.Render(callback=test)

This way, when the button is pressed, it will execute the test function defined in main.py.

class.py

def button_callback():
    # Call the test function in main.py
    test()

def Render(self):
    button = tk.Button(
        text=txt,
        font=self.font,
        command=self.button_callback,  # Set the command to the callback function in this file
        bg=self.colour
    )
  • This worked! Thank you very much, I should have thought of just parsing the method through to it :D – Lordimass Jun 27 '23 at 07:27
  • 1
    This answer looks like ChatGPT – DavidW Jun 28 '23 at 05:41
  • @DavidW, I noticed that you have been commenting on many posts that are ChatGPT generated. Have you been flagging them? – jared Jun 28 '23 at 05:54
  • 1
    @jared I haven't been flagging them any more since Stack Overflow management blocked moderators from using the flags. I've just been commenting and voting since this is all we can usefully do right now unfortunately – DavidW Jun 28 '23 at 07:19
  • 1
    Ah dang, probably should have recognised that it was ChatGPT, I have un-upvoted, it did solve my problem though so I'll leave it as accepted. – Lordimass Jun 28 '23 at 09:38
  • @DavidW What do you mean moderators cannot flag? Shouldn't moderators have more power? – jared Jun 28 '23 at 14:27
  • Ok sorry i didnt know that using GPT is not allowd, i was out from the platforme, i admit that i used GPT – Abdul Wahab Rana Jun 28 '23 at 14:30
  • i did not used GPT but other AI tool but still i am sorry. I am ok if want me to remove the answer – Abdul Wahab Rana Jun 28 '23 at 14:31