user5081090
user5081090

Reputation:

Passing variables between two windows Tkinter

I posted something about this earlier and how a response which I thought solved my issue but it didn't fully solve it. Basically, I'm looking for a way to pass StringVar() from my first method called 'main_frame' to my second method called 'result_frame' when the button in the 'main_frame' method is clicked.

from Tkinter import *
import Forecast


class Frames(object):

    def __init__(self):
        pass

    def main_frame(self):
        root = Tk()
        root.title('WeatherMe')
        root.geometry('300x100')

        query = StringVar()

        Label(root, text='Enter a city below').pack()

        Entry(root, textvariable=query).pack()

        Button(root, text="Submit", command=self.result_frame).pack()

        root.mainloop()

    def result_frame(self):
        result = Toplevel()
        result.title('City')
        result.geometry('1600x150')

        forecast = Forecast.GetWeather('City Here').fetch_weather()

        Label(result, text='1-3 DAY: \n' + forecast[0]).pack()
        Label(result, text='4-7 DAY: \n' + forecast[1]).pack()
        Label(result, text='8-10 DAY: \n' + forecast[2]).pack()

        Button(result, text="OK", command=result.destroy).pack()

As you can see one window will have an Entry box where you can type in a city. The entry will save it's contents to StringVar(). I want my button in the 'main_frame' method to call 'result_frame' and use the value located in my first window. If I add a parameter to the second method then I'd have to specify it in my button right away and that will of course immediately call it when the program is opened which I want to avoid. I apologize if this is an easy solution I'm just missing here, I've struggled with this for a while with no solution. I was told last time I posted this to only call one instance of Tk() which I have done here and used Toplevel() for my additional window but I'm still having issues passing the variable from one window to the other.

Upvotes: 0

Views: 12095

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90889

Your self (the object) is the same in both __init__() as well as result_frame() , so you can set the StringVar() as an instance variable, and then in the result_frame() method, you can get the StringVar() from that instance variable.

Also, I would like to suggest that you should not do root.mainloop() within the method of your class, that can cause the object to be never completely be available outside the class until you actually close the tkinter application (if called from within __init__() or so). Instead create root outside the class and pass it in as a variable. Example -

from Tkinter import *
import Forecast

class Frames(object):

    def __init__(self):
        pass

    def main_frame(self, root):
        root.title('WeatherMe')
        root.geometry('300x100')

        self.query = StringVar()

        Label(root, text='Enter a city below').pack()

        Entry(root, textvariable=self.query).pack()

        Button(root, text="Submit", command=self.result_frame).pack()

    def result_frame(self):
        result = Toplevel()
        result.title('City')
        result.geometry('1600x150')

        print(self.query.get()) #This would print the StringVar's value , use this in whatever way you want.

        forecast = Forecast.GetWeather('City Here').fetch_weather()

        Label(result, text='1-3 DAY: \n' + forecast[0]).pack()
        Label(result, text='4-7 DAY: \n' + forecast[1]).pack()
        Label(result, text='8-10 DAY: \n' + forecast[2]).pack()

        Button(result, text="OK", command=result.destroy).pack()


root = Tk()
app = Frames()
app.main_frame(root)
root.mainloop()

Upvotes: 2

Related Questions