Python Forum
[Tkinter] Unable to Obtain values from one Tkinter Program into another - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Unable to Obtain values from one Tkinter Program into another (/thread-12429.html)



Unable to Obtain values from one Tkinter Program into another - nilaybnrj - Aug-24-2018

Hi Team

Here I am facing one issue. I am basically trying to create one basic automation app for desktop. In which one main window will be there with several scenarios & if we choose one scenario & then click submit button, another tkinter window will appear asking for specific inputs from user & after users enter the inputs & press submit button the control will be back to main window & the parameters given by users will be stored in variables defined inside main program. When I am trying to get the output from inside of the side program, the output is coming properly. But when I am trying to call it from the main program only default values of those variables are coming, not values updated by users.

Side Program
import Tkinter as tk


class Reboot_param:
    def __init__(self,master):
        self.time_to_wait=0
        self.master=master
        self.label_ttw = tk.Label(self.master,text="Time to Wait")
        self.label_ttw.grid(row=0,column=0)
        self.ttw = tk.IntVar()
        self.entry_ttw = tk.Entry(self.master,textvariable=self.ttw,width=20)
        self.entry_ttw.grid(row=0,column=2)
        self.button_submit = tk.Button(self.master,text="Submit",command = self.getvar)
        self.button_submit.grid(row=1,column=1)
        self.button_close = tk.Button(self.master, text="Close", command=self.master.destroy)
        self.button_close.grid(row=1, column=2)

    def getvar(self):
        self.time_to_wait=self.ttw.get()


if __name__ == '__main__':
    window=tk.Tk()
    reboot = Reboot_param(window)
    window.mainloop()
    print reboot.time_to_wait
Output:
90
Main Program
import Tkinter as tk
from multiprocessing import process
from devices import DeviceSelection
from voice_setup import voiceSetup
import reboot_param as reb
from message_params import MessageParams


class automation_suite:
    def __init__(self,master):
        self.tel_no = 0
        self.iteration = 0
        self.text = ""
        self.call_duration = 0
        self.time_to_wait = 0
        self.master=master
        self.scenariolist=["Voice Call","VT Call","Reboot","SMS"]
        self.frame_scenario = tk.Frame(self.master)
        self.frame_scenario.pack(side=tk.TOP,fill=tk.X,expand=tk.YES,padx=10,pady=10)
        self.label_scenario = tk.Label(self.frame_scenario,text="Scenario: ",width=10)
        self.label_scenario.pack(side=tk.LEFT,fill=tk.X)
        self.scenarios=tk.StringVar()
        self.scenarios.set('')
        self.scenario_select_menu=tk.OptionMenu(self.frame_scenario,self.scenarios,*self.scenariolist)
        self.scenario_select_menu.pack(side=tk.LEFT,fill=tk.X,padx=10)
        self.frame_data = tk.Frame(self.master)
        self.frame_data.pack(side=tk.TOP,fill=tk.X,expand=tk.YES)
        self.devicelist=["Device 1","Device 2","Device 3"]
        self.label_device= tk.Label(self.frame_scenario,text="Device: ",width=10)
        self.label_device.pack(side=tk.LEFT,fill=tk.X)
        self.device=tk.StringVar()
        self.device.set('')
        self.device_select_menu=tk.OptionMenu(self.frame_scenario,self.device,*self.devicelist)
        self.device_select_menu.pack(side=tk.LEFT,fill=tk.X,padx=10)
        self.frame_button = tk.Frame(window)
        self.frame_button.pack(side=tk.TOP,fill=tk.X)
        self.button = tk.Button(self.frame_button,text="Start Test Execution",width=15,command=self.start_execute)
        self.button.pack(side=tk.LEFT,padx=15,pady=15)
        self.button2 = tk.Button(self.frame_button, text="Detect Devices", width=15,command=self.get_devices)
        self.button2.pack(side=tk.LEFT, padx=15, pady=15)

    def start_execute(self):
        root = tk.Tk()
        if self.scenarios.get()=="Voice Call" or self.scenarios.get()=="VT Call":
            voice=voiceSetup()
            print voice.data_list
            self.tel_no=voice.data_list[0]
            self.iteration = voice.data_list[1]
            self.call_duration = voice.data_list[2]
            self.time_to_wait=voice.data_list[3]

        elif self.scenarios.get()=="Reboot":
            reboot = reb.Reboot_param(root)
            root.mainloop()
            print reboot.time_to_wait

        elif self.scenarios.get()=="SMS":
            message = MessageParams()
            self.tel_no = message.data_list[0]
            self.iteration = message.data_list[1]
            self.time_to_wait = message.data_list[2]
            self.text = message.data_list[3]

    def get_devices(self):
        self.device_select_menu['menu'].delete(0, tk.END)
        self.devicelist = DeviceSelection().list
        for item in self.devicelist:
            self.device_select_menu['menu'].add_command(label=item, command=tk._setit(self.device, item))


if __name__ == '__main__':
    window = tk.Tk()
    auto = automation_suite(window)
    window.mainloop()
Output:
0
Please help. Thanks in advance


RE: Unable to Obtain values from one Tkinter Program into another - Gribouillis - Aug-24-2018

You must not call .mainloop() more than once. You could read this tutorial at effbot to create a dialog window. Look at the tkSimpleDialog.py file the key steps are in the dialog class' __init__() method, namely the calls to grab_set(), focus_set(), wait_window(). You may need to adapt this code to python 3 (which is easy).