Nemo XXX
Nemo XXX

Reputation: 691

Python: Tkinter OK button callback function

I'm working on my very first Python GUI and I'm trying to modify this tkinter example, but I simply cannot figure out how to write a callback function for the OK button that will pass on the entered value to the main program.

#!/usr/bin/python
# -*- coding: utf-8 -*-

from Tkinter import Tk, BOTH, StringVar, IntVar
from ttk import Frame, Button, Style, Label, Entry

class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)   
        self.parent = parent
        self.initUI()

    def initUI(self):
        self.parent.title("Get Value")
        self.style = Style()
        self.style.theme_use("default")
        self.pack(fill=BOTH, expand=1)

        valueLabel = Label(self, text="Value: ")
        valueLabel.place(x=10, y=10)
        value=StringVar(None)
        value.set("this is the default value")
        valueEntry=Entry(self, textvariable=value)
        valueEntry.place(x=70, y=10)

        quitButton = Button(self, text="Quit", command=self.quit)
        quitButton.place(x=10, y=50)

        okButton = Button(self, text="OK", command=self.quit)
        okButton.place(x=120, y=50)

def main():

    root = Tk()
    root.geometry("220x100+300+300")
    app = Example(root)
    root.mainloop()

if __name__ == '__main__':
    main()  

I've read a gazillion of tutorials, but none of them explains this clearly. Theoretically, I should be able to get the selected value with value.get(), but I keep getting error messages no matter where I put it. Also, AFAIK, I should be able to define a default value with value.set(), but this doesn't seem to have an effect, since the text box is empty when I run the program.

  1. What is the easiest way to pass on values to the main python program after root.mainloop() terminates? (The actual dialog box contains several entry boxes for entering string and integer values.)

    I.e. I want to be able to use something like:

    root = Tk()
    root.geometry("220x100+300+300")
    app = Example(root)
    root.mainloop()
    print value
    print value2        
    print value3
    
  2. How do I define default values for entry boxes?

Upvotes: 0

Views: 5295

Answers (2)

Kidus
Kidus

Reputation: 1843

Change every occurrence of the value variable with self.value. This should fix it and the default value will be displayed.

UPDATE

from Tkinter import Tk, BOTH, StringVar, IntVar
from ttk import Frame, Button, Style, Label, Entry

class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)   
        self.parent = parent
        self.initUI()

    def showMe(self):
        print(self.value.get())

    def initUI(self):
        self.parent.title("Get Value")
        self.style = Style()
        self.style.theme_use("default")
        self.pack(fill=BOTH, expand=1)

        valueLabel = Label(self, text="Value: ")
        valueLabel.place(x=10, y=10)
        self.value=StringVar(None)
        self.value.set("this is the default value")
        valueEntry=Entry(self, textvariable=self.value)
        valueEntry.place(x=70, y=10)

        quitButton = Button(self, text="Quit", command=self.quit)
        quitButton.place(x=10, y=50)

        okButton = Button(self, text="OK", command=self.showMe)
        okButton.place(x=120, y=50)




def main():

    root = Tk()
    root.geometry("220x100+300+300")
    app = Example(root)
    root.mainloop()


if __name__ == '__main__':
    main()  

Upvotes: 2

DorinPopescu
DorinPopescu

Reputation: 725

Both your quitButton and okButton call the self.quit functions. So no mater what value you enter when you press the OK button you are calling the quit function which has its own problems as well outside the scope of your question. Try to define value as self.value and make the okButton call a function that does: print self.value.get().

Upvotes: 0

Related Questions