Luke.py
Luke.py

Reputation: 1005

Returning variables from Tkinter GUI

I'm looking at using a simple, currently ugly, GUI built with Tkinter to attain two variables from the user. Namely a file path and a choice from a dropdown (OptionMenu).

The variables selected will be used later in the Python script, which is where I'm running into difficulty. Put simply, how to asign the users choices to the variables: Carrier, Path.

Please see below for sample code:

from Tkinter import *
from tkFileDialog import askopenfilename

def Choose_Path():
    Tk().withdraw()
    return askopenfilename()


root = Tk()
root.geometry('400x400')
root.configure(background='#A2B5CD')

C_Label = Label(root, text='Carrier Choice:', bg='#A2B5CD', fg='black',font=('Calibri', 12))
C_Label.grid(row=0,sticky=W, padx =10)
I_Label = Label(root, text='Invoice Path:', bg='#A2B5CD', fg='black',font=('Calibri', 12))
I_Label.grid(row=1, sticky=W, padx =10)


var = StringVar(root)
var.set('Choose Carrier...')
option = OptionMenu(root, var, 'DHL','DPD','DX','Fedex','Geodis','Hermes','WN Direct')
option.config(relief=RAISED, highlightbackground='#A2B5CD')
option.grid(row=0,column=1, sticky=W, pady = 10)

browser = Button(root, text = 'Browse Invoice...', command=Choose_Path)
browser.grid(row=1, column=1, sticky=W, pady=10)


Button(root, text='Accept and Close').grid(column=1, sticky=S)

root.mainloop()

Any feedback would be appreciated. Thanks in advance.

Upvotes: 0

Views: 1025

Answers (2)

Luke.py
Luke.py

Reputation: 1005

Through a combination of your feedback and a little more playing around with an extra function, I now seem to be getting the results that I need. See below for what it looks like now.

from Tkinter import *
from tkFileDialog import askopenfilename
path = []

def Choose_Path():
    Tk().withdraw()
    path.append(askopenfilename())

def CloseGUI():
    root.quit()
    root.destroy()

root = Tk()
root.geometry('400x400')
root.configure(background='#A2B5CD')

C_Label = Label(root, text='Carrier Choice:', bg='#A2B5CD', fg='black',font=('Calibri', 12))
C_Label.grid(row=0,sticky=W, padx =10)
I_Label = Label(root, text='Invoice Path:', bg='#A2B5CD', fg='black',font=('Calibri', 12))
I_Label.grid(row=1, sticky=W, padx =10)

var = StringVar(root)
var.set('Choose Carrier...')
option = OptionMenu(root, var, 'DHL','DPD','DX','Fedex','Geodis','Hermes','WN Direct')
option.config(relief=RAISED, highlightbackground='#A2B5CD')
option.grid(row=0,column=1, sticky=W, pady = 10)

browser = Button(root, text = 'Browse Invoice...', command=Choose_Path)
browser.grid(row=1, column=1, sticky=W, pady=10)
b1 = Button(root, text='Accept and Close', command = CloseGUI).grid(column=1, sticky=S)
mainloop()

print var.get()
print path

Thanks for your help! +1

Upvotes: 2

acdr
acdr

Reputation: 4716

Two issues:

-You're going to have to figure out when to end your root's mainloop. From the moment you call root.mainloop(), currently the program will not advcance to the next line (which you don't have, but I assume you will in your final program) until you close the Tk window.

-After the mainloop has ended, you need to have your variable values somewhere. Currently, the option object (which is an OptionMenu instance) will contain the value if your carrier, so you can just do something like option.get(). The filename is slightly more complicated, because you don't store that somewhere: you return it from Choose_Path() but the return value isn't stored anywhere. Probably you're going to have to store this value in a global. (This storing has to happen within Choose_Path, e.g. FileName = askopenfilename() instead of return askopenfilename()).

Upvotes: 0

Related Questions