[Python] Tkinter Class Import Module Issue

Discussão em 'Python' iniciado por Stack, Maio 28, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to keep my code clean by separating the GUI from the logic.

    Inside of the 'main.py' file, I'd like to call functions from other files that are imported to build the GUI.

    The problem is that I cannot figure out how to build a GUI from the 'main.py' file when I try to call another file as an imported module.

    Here's what I have in the 'main.py' file:

    from tkinter import *
    import create_btn

    class Main(Tk):
    def __init__(self):
    super().__init__()
    self.title('Main Window')
    self.geometry('600x400')
    self.eval('tk::placeWindow . center')


    if __name__ == '__main__':
    app = Main()
    app.mainloop()


    And here is what I have in the 'create_btn.py' file:

    from tkinter import *

    def createBTN(self):
    self.b1 = Button(root, text='B1')
    self.b1.pack()


    So, how exactly can I build a simple button from another file that I want to import into the 'main.py', or in other words, how do I get the 'create_btn.py' file to build the button inside of the 'main.py' file? A simple example would be greatly appreciated.

    Continue reading...

Compartilhe esta Página