Python Tkinter GUI application query Python Tkinter GUI application query tkinter tkinter

Python Tkinter GUI application query


This might not help you completely, but this is what I use. I divide my tkinter code into 2 files. First gui.py contains the GUI components (widgets) and the second methods.py contains the methods.Both the files should be in same directory.

Here is an example of a simple app that changes the label on a button click. The method change() is stored in a different file.

gui.py

from tkinter import *from tkinter import ttkfrom methods import change   #Using absolute import instead of wildcard importsclass ClassNameGoesHere:      def __init__(self,app):    self.testbtn = ttk.Button(app,text="Test",command = lambda: change(self))     #calling the change method.    self.testbtn.grid(row=0,column=0,padx=10,pady=10)    self.testlabel = ttk.Label(app,text="Before Button Click")    self.testlabel.grid(row=1,column=0,padx=10,pady=10)def main():  root = Tk()  root.title("Title Goes Here")  obj = ClassNameGoesHere(root)  root.mainloop()  if __name__ == "__main__":  main()

methods.py

from tkinter import *from tkinter import ttkdef change(self):    self.testlabel.config(text="After Button Click")