How to terminate a tkinter loop and use a returned value
Question:
I’ve setup a GUI to collect a file location. I then went to terminate the tkinter loop so I can use the submitted value to generate a file. With my current setup below the loop is not terminating.
I’ve had a few goes at moving around where root.mainloop() line is being called but not had any success.
from tkinter import *
from tkinter import filedialog
class InputForm:
def __init__(self, master):
# Cashflows 1 Input
self.file_path1 = StringVar()
Label(master, text="Input path 1:", fg='black', font=("Helvetica", 12)).place(x=10, y=10)
self.file_path1 = Entry(master, text="Input path 1", bd=5, textvariable = self.file_path1)
self.file_path1.place(x=120, y=10)
Button(master, text = "Select", command = self.get_file_path1).place(x= 260, y = 10)
master.title('Cashflow Summariser')
master.geometry("310x160+10+20")
#This needs to be simplified -> work out how to pass ByRef
def get_file_path1(self):
self.file_path = filedialog.askopenfilename()
self.file_path1.set(self.file_path)
def submit(self):
path1 = self.file_path1.get()
paths = [path1]
while("" in paths):
paths.remove("")
self.file_paths = paths
self.master.destroy
class CashflowCombiner:
def __init__(self, file_path):
print(file_path)
def main():
root = Tk()
inputForm = InputForm(root)
root.mainloop()
file_paths = inputForm.file_paths
print(file_paths)
if __name__ == '__main__': main()
Answers:
You can terminate a tkinter loop by exiting the application either manually or by the user clicking the exit button.
You also have an issue with overwriting the self.file_path1
variable which will raise an exception when you try to set the value in the get_file_path1
method.
Here is an example of how you could do it.
from tkinter import *
from tkinter import filedialog
class InputForm:
def __init__(self, master):
self.master = master # set master as an instance attribute
self.file_path_var = StringVar() # renamed this so it doesn't get overwritten
Label(master, text="Input path 1:", fg='black', font=("Helvetica", 12)).place(x=10, y=10)
self.file_path1 = Entry(master, text="Input path 1", bd=5, textvariable = self.file_path_var)
self.file_path1.place(x=120, y=10)
Button(master, text = "Select", command = self.get_file_path1).place(x= 260, y = 10)
self.master.title('Cashflow Summariser')
self.master.geometry("310x160+10+20")
self.file_paths = [] # set the self.file_paths attribute to empty list
#This needs to be simplified -> work out how to pass ByRef
def get_file_path1(self):
self.file_path = filedialog.askopenfilename()
self.file_path_var.set(self.file_path)
self.file_paths.append(self.file_path) # append the path to self.file_paths
#self.master.quit()
# uncommenting the above line will exit the mainloop after getting
# the file path from the user or you can leave it commented and
# wait for the user to exit the window manually.
def main():
root = Tk()
inputForm = InputForm(root)
root.mainloop()
file_paths = inputForm.file_paths
print(file_paths)
if __name__ == '__main__': main()
Most of the above seems rather unnecessary though… If all you want to do is use the file dialog, then you could probably just do something like this:
from tkinter import *
from tkinter import filedialog
def main():
Tk()
file_path = filedialog.askopenfilename()
print(file_path)
if __name__ == '__main__': main()
I’ve setup a GUI to collect a file location. I then went to terminate the tkinter loop so I can use the submitted value to generate a file. With my current setup below the loop is not terminating.
I’ve had a few goes at moving around where root.mainloop() line is being called but not had any success.
from tkinter import *
from tkinter import filedialog
class InputForm:
def __init__(self, master):
# Cashflows 1 Input
self.file_path1 = StringVar()
Label(master, text="Input path 1:", fg='black', font=("Helvetica", 12)).place(x=10, y=10)
self.file_path1 = Entry(master, text="Input path 1", bd=5, textvariable = self.file_path1)
self.file_path1.place(x=120, y=10)
Button(master, text = "Select", command = self.get_file_path1).place(x= 260, y = 10)
master.title('Cashflow Summariser')
master.geometry("310x160+10+20")
#This needs to be simplified -> work out how to pass ByRef
def get_file_path1(self):
self.file_path = filedialog.askopenfilename()
self.file_path1.set(self.file_path)
def submit(self):
path1 = self.file_path1.get()
paths = [path1]
while("" in paths):
paths.remove("")
self.file_paths = paths
self.master.destroy
class CashflowCombiner:
def __init__(self, file_path):
print(file_path)
def main():
root = Tk()
inputForm = InputForm(root)
root.mainloop()
file_paths = inputForm.file_paths
print(file_paths)
if __name__ == '__main__': main()
You can terminate a tkinter loop by exiting the application either manually or by the user clicking the exit button.
You also have an issue with overwriting the self.file_path1
variable which will raise an exception when you try to set the value in the get_file_path1
method.
Here is an example of how you could do it.
from tkinter import *
from tkinter import filedialog
class InputForm:
def __init__(self, master):
self.master = master # set master as an instance attribute
self.file_path_var = StringVar() # renamed this so it doesn't get overwritten
Label(master, text="Input path 1:", fg='black', font=("Helvetica", 12)).place(x=10, y=10)
self.file_path1 = Entry(master, text="Input path 1", bd=5, textvariable = self.file_path_var)
self.file_path1.place(x=120, y=10)
Button(master, text = "Select", command = self.get_file_path1).place(x= 260, y = 10)
self.master.title('Cashflow Summariser')
self.master.geometry("310x160+10+20")
self.file_paths = [] # set the self.file_paths attribute to empty list
#This needs to be simplified -> work out how to pass ByRef
def get_file_path1(self):
self.file_path = filedialog.askopenfilename()
self.file_path_var.set(self.file_path)
self.file_paths.append(self.file_path) # append the path to self.file_paths
#self.master.quit()
# uncommenting the above line will exit the mainloop after getting
# the file path from the user or you can leave it commented and
# wait for the user to exit the window manually.
def main():
root = Tk()
inputForm = InputForm(root)
root.mainloop()
file_paths = inputForm.file_paths
print(file_paths)
if __name__ == '__main__': main()
Most of the above seems rather unnecessary though… If all you want to do is use the file dialog, then you could probably just do something like this:
from tkinter import *
from tkinter import filedialog
def main():
Tk()
file_path = filedialog.askopenfilename()
print(file_path)
if __name__ == '__main__': main()