why can i not call this variable from a class in a different module? why can i not call this variable from a class in a different module? tkinter tkinter

why can i not call this variable from a class in a different module?


Not entirely sure but I guess that your issue is related to the following line

if __name__ == "__main__":

meaning that your class GUI is not instantiated as can be seen in [0]. Moving the creation of the GUI class, that is using the following code snippet

import tkinter as tkclass GUI(tk.Tk):    def __init__(self):        tk.Tk.__init__(self)        tk.Label(self,text="Testers Name").grid()        self.testers_name = tk.Entry(self,text="Testers Name").grid()        tk.Label(self,text="Report Name").grid()        self.report_name = tk.Entry(self,text="Report Name").grid()        submitButton = tk.Button(self, text="Test Selected",command=self.query_checkbuttons)        submitButton.grid()    def query_checkbuttons(self):        some_stuff_blah_blah_blah            gui = GUI()print(gui.report_name)gui.mainloop()

will at least allow you to import the variable gui from that module.

However, I strongly recommand to not instantiate the class at the end of the module because it is reinstantiated every time your import that module!


EDIT1:

I would do it as follows:

thiscode.py

import tkinter as tkclass GUI(tk.Tk):    def __init__(self):        tk.Tk.__init__(self)        tk.Label(self,text="Testers Name").grid()        self.testers_name = tk.Entry(self,text="Testers Name").grid()        tk.Label(self,text="Report Name").grid()        self.report_name = tk.Entry(self,text="Report Name").grid()        submitButton = tk.Button(self, text="Test Selected",command=self.query_checkbuttons)        submitButton.grid()    def query_checkbuttons(self):        some_stuff_blah_blah_blah            def start_gui():    gui = GUI()    # print(gui.report_name)    gui.mainloop()if __name__ == "__main__":    start_gui()

and then in pdf.py

def myFirstPage(canvas, doc):    import gui    gui.start_gui()  # --> manually open up the gui after import <--    # print(gui.report_name)    canvas.saveState()    canvas.setFont('Times-Bold',16)    canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title)    canvas.setFont('Times-Roman',9)    canvas.drawString(inch, 0.75 * inch, "First Page / %s" % pageinfo)    canvas.restoreState()

Now let me clarify that when you call thiscode.py via python thiscode.py you will end up with exactly one GUI being opened up. This is however not the case if you import the module as can be seen in pdf.py. Hence, we directly instantiate one GUI object via function invocation which must be done manually (see line 3 with the unmistakable inline comment in pdf.py).


[0] https://docs.python.org/3/tutorial/modules.html#executing-modules-as-scripts


The whole point of the if __name__ block is to only execute its contents when that file is run directly, and not when it is imported. So if you do want to import the gui instance, you should not create it within that block.