Hi, all. I'm writing a little GUI thing to allow a user to specify variables in an xml file.
I am battling to work out how to return all the .get() values from the Entry widgets back to a main application.
The GUI is done as a function as the moment to be called from the main programme
(Sorry, haven't got my head around classes yet which I why I am doing it this way).

The problem is that I cannot figure out where to put the return statement (or work out if this is what I should be doing).
If I place the return statement outside the mainloop the GUI works but no value is returned, if I put it before mainloop then the GUI doesn't display.
I'm trying to avoid having a gazillion global variables floating around and was hoping to pack all these get() values into a dictionary which I could access more easily.

I'm stumped and cannot find any reference to how to do this by googling.

Below is my rough code:

from Tkinter import *

def setupwin():
    
    

    setupwin=Tk()

    setupwin.title("Setup Options")

    questionframe=Frame(setupwin)
    questionframe.grid(column=0,row=0)
    
    labelsize=Entry(questionframe,width=10)
    labelsize.grid(column=1,row=1)
    labelsizeLabel=Label(questionframe,text="labelsize")
    labelsizeLabel.grid(column=0,row=1)
   
    labeltext=Entry(questionframe,width=10)
    labeltext.grid(column=1,row=2)
    labeltextLabel=Label(questionframe,text="labeltext")
    labeltextLabel.grid(column=0,row=2)

    labelrounded=Entry(questionframe,width=10)
    labelrounded.grid(column=1,row=3)
    labelroundedLabel=Label(questionframe,text="labelrounded")
    labelroundedLabel.grid(column=0,row=3)

    labeltextcolor=Entry(questionframe,width=10)
    labeltextcolor.grid(column=1,row=4)
    labeltextcolorLabel=Label(questionframe,text="labeltextcolor")
    labeltextcolorLabel.grid(column=0,row=4)

    labeltextopacity=Entry(questionframe,width=10)
    labeltextopacity.grid(column=1,row=5)
    labeltextopacityLabel=Label(questionframe,text="labeltextopacity")
    labeltextopacityLabel.grid(column=0,row=5)


    labelbgcolor=Entry(questionframe,width=10)
    labelbgcolor.grid(column=1,row=6)
    labelbgcolorLabel=Label(questionframe,text="labelbgcolor")
    labelbgcolorLabel.grid(column=0,row=6)


    labelbgopacity=Entry(questionframe,width=10)
    labelbgopacity.grid(column=1,row=7)
    labelbgopacityLabel=Label(questionframe,text="labelbgopacity")
    labelbgopacityLabel.grid(column=0,row=7)


    tooltiptextsize=Entry(questionframe,width=10)
    tooltiptextsize.grid(column=1,row=8)
    tooltiptextsizeLabel=Label(questionframe,text="tooltiptextsize")
    tooltiptextsizeLabel.grid(column=0,row=8)



    tooltiptextcolor=Entry(questionframe,width=10)
    tooltiptextcolor.grid(column=1,row=9)
    tooltiptextcolorLabel=Label(questionframe,text="tooltiptextcolor")
    tooltiptextcolorLabel.grid(column=0,row=9)


    tooltipbgcolor=Entry(questionframe,width=10)
    tooltipbgcolor.grid(column=1,row=10)
    tooltipbgcolorLabel=Label(questionframe,text="tooltipbgcolor")
    tooltipbgcolorLabel.grid(column=0,row=10)

    tooltiprounded=Entry(questionframe,width=10)
    tooltiprounded.grid(column=1,row=11)
    tooltiproundedLabel=Label(questionframe,text="tooltiprounded")
    tooltiproundedLabel.grid(column=0,row=11)


    tooltipbgopacity=Entry(questionframe,width=10)
    tooltipbgopacity.grid(column=1,row=12)
    tooltipbgopacityLabel=Label(questionframe,text="tooltipbgopacity")
    tooltipbgopacityLabel.grid(column=0,row=12)

    tooltiptextopacity=Entry(questionframe,width=10)
    tooltiptextopacity.grid(column=1,row=13)
    tooltiptextopacityLabel=Label(questionframe,text="tooltiptextopacity")
    tooltiptextopacityLabel.grid(column=0,row=13)

    tooltiptextpadding=Entry(questionframe,width=10)
    tooltiptextpadding.grid(column=1,row=14)
    tooltiptextpaddingLabel=Label(questionframe,text="tooltiptextpadding")
    tooltiptextpaddingLabel.grid(column=0,row=14)

    def killwindow():
        questionframe.destroy()
        


    doneButton=Button(questionframe,text="Done",command=killwindow)
    doneButton.grid(column=1,row=15)
    
    for child in questionframe.winfo_children(): child.grid_configure(padx=5, pady=5)

   

    values={'labelsize':labelsize.get(),'labeltext':labeltext.get(),'labelrounded':labelrounded.get(),'labeltextcolor':labeltextcolor.get(),'labeltextopacity':labeltextopacity.get(),'labelbgcolor,':labelbgcolor.get(),'labelbgopacity':labelbgopacity.get(),'tooltiptextsize':tooltiptextsize.get(),'tooltiptextcolor':tooltiptextcolor.get(),'tooltipbgcolor':tooltipbgcolor.get(),'tooltiprounded':tooltiprounded.get(),'tooltipbgopacity':tooltipbgopacity.get(),'tooltiptextopacity':tooltiptextopacity.get(),'tooltiptextpadding':tooltiptextpadding.get()}
     
   
   
    setupwin.mainloop()
    return values

setupwin()

Recommended Answers

All 2 Replies

The easiest way is to use a class and define the dictionary as self.some_name so you can access it inside or outside the class. A simple example:

class TestClass:

    def __init__(self):
        self.test_name = "test name from class"

    def print_object_variable(self):
        print "variable within class -->", self.test_name

x = TestClass()
print "outside class -->", x.test_name
x.print_object_variable()

Thanks. Looks like I'll just have to get over my terror of classes :)

I'll give your solution a go.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.