Issue
I realize this is probably really simple, I do it in Matlab all the time. Right now, all I want to do is use a button in tkinter to open a file dialog, choose a file, and store the pathname in a variable.
class Functions:
def FileDialog(Pathname):
Pathname = tkFileDialog.askopenfilename()
Funks = Functions()
Btn1 = tkinter.Button(MainWindow, text = "Browse", command = Funks.FileDialog)
After the function is done, where does "Pathname" go? How can I view it in the variables explorer?
Update: I've structured the code differently now and here it is in its entirety
##########################################
import tkinter as tk #
from tkinter import Frame #
import tkinter.filedialog as tkFileDialog#
##########################################
class Window(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.MainWindow()
def MainWindow(self):
self.master.title("The SHIFT Show"); self.pack()
Btn1 = tk.Button(self, text = "Browse", command = self.FileDialog)
Btn1.place(x = 0, y = 0); Btn1.pack()
Btn2 = tk.Button(self, text = "Close Window", command = self.Exit)
Btn2.place(x = 0, y = 0); Btn2.pack()
Btn3 = tk.Button(self, text = "Pathname?", command = self.PathTest)
Btn3.place(x = 0, y = 0); Btn3.pack()
def FileDialog(self):
Pathname = tkFileDialog.askopenfilename()
return Pathname
def Exit(self):
exit() #DOESNT WORK
def PathTest(self,Pathname):
print(Pathname)
root = tk.Tk()
app = Window(root)
root.geometry("500x500+2500+1100")
root.mainloop()
Solution
I got it to do what I wanted. yay.
##########################################
import tkinter as tk #
from tkinter import Frame #
import tkinter.filedialog as tkFileDialog#
##########################################
class Window(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.MainWindow()
def MainWindow(self):
self.master.title("The SHIFT Show"); self.pack()
# BUTTONS
## File dialog button
Btn1 = tk.Button(self, text = "Browse", command = self.FileDialog)
Btn1.place(x = 0, y = 0); Btn1.pack()
## Close window button
Btn2 = tk.Button(self, text = "Close Window", command = self.Exit)
Btn2.place(x = 0, y = 0); Btn2.pack()
## Test button for....things
Btn3 = tk.Button(self, text = "Pathname?", command = self.PathTest)
Btn3.place(x = 0, y = 0); Btn3.pack()
def FileDialog(self):
global Pathname
folderpath = tkFileDialog.askdirectory()
Pathname = folderpath
def Exit(self):
exit() # STILL DOESNT WORK
def PathTest(self):
print(Pathname) # Outputs the folder path to the console (so I know it's there)
root = tk.Tk()
app = Window(root)
root.geometry("500x500+2500+1100")
root.mainloop()
Answered By - Kris Hoffman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.