Python Forum
[Tkinter] Unable to Obtain values from one Tkinter Program into another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Unable to Obtain values from one Tkinter Program into another
#1
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
Reply
#2
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).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Noob question:Using pyttsx3 with tkinter causes program to stop and close, any ideas? Osman_P 4 5,292 Nov-14-2020, 10:51 AM
Last Post: Osman_P
  [Tkinter] Tkinter delete values in Entries, when I'm changing the Frame robertoCarlos 11 5,801 Jul-29-2020, 07:13 PM
Last Post: deanhystad
  [Tkinter] Problems to display Web Scraping values in Tkinter Lucas_Ribeiro 0 1,564 May-07-2020, 12:36 AM
Last Post: Lucas_Ribeiro
  [Tkinter] Tkinter adding entry values scratchmyhead 1 2,199 May-04-2020, 05:21 AM
Last Post: Yoriz
  Tkinter:Unable to bind and unbind function with a button shallanq 2 5,029 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  Unable fetch fucntion data in class in tkinter jenkins43 2 3,882 Nov-30-2019, 09:47 PM
Last Post: jenkins43
  Unable to put background image on Tkinter Frame jenkins43 2 8,749 Nov-27-2019, 11:38 AM
Last Post: jenkins43
  tkInter and Pillow don't display any images in GUIs - program gives errors instead SomeRandomGuy 9 10,773 Oct-29-2019, 02:57 PM
Last Post: SomeRandomGuy
  Unable to update or refresh label text in tkinter jenkins43 3 6,598 Jul-24-2019, 02:09 PM
Last Post: Friend
  Exiting/killing a program with tkinter but leaving the graphic on the screen jpezz 3 3,968 Apr-07-2019, 02:13 PM
Last Post: jpezz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020