Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data in GUI's Main window from a child window?

Tags:

python

tkinter

I am trying to learn Python and trying to built up a GUI. I am facing a problem which is as follows:

In my Code I am trying to get the inputs from a user when 'Ausgewahlte' Check box is selected, which upon selection open up the child window and allow the user to enter the data. Now I have to tell in the main GUI to take the data either from the Child window or use the default data as per the selection between 'Alle' or 'Ausgewahlte'.

Firstly, I am not able to use the data defined in Child window in a main GUI.

It will be great if anyone among you will be able to help.

Thanking you in anticipation !!!

Regards

`

import Tkinter
import tkSimpleDialog

class PST_TEG(Tkinter.Tk):

    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):

        # Label
         self.grid()
         label = Tkinter.Label(self,text="Projekt Root Ordner Name :",anchor="w").grid(column=0,row=0,columnspan=1,sticky='EW')
         label = Tkinter.Label(self,text="Projekt Name :",anchor="w").grid(column=0,row=1,columnspan=1,sticky='EW')
         label = Tkinter.Label(self,text="Experiment Name :",anchor="w").grid(column=0,row=2,columnspan=1,sticky='EW')        
         label = Tkinter.Label(self,text="Plattform Name :",anchor="w").grid(column=0,row=3,columnspan=1,sticky='EW')
         label = Tkinter.Label(self,text="Anzahl der Szenarien :",anchor="w").grid(column=0,row=4,columnspan=1,sticky='EW')
         label = Tkinter.Label(self,text="Temperatur Kuhlwasser :",anchor="w").grid(column=0,row=5,columnspan=1,sticky='EW')
         label = Tkinter.Label(self,text="Szenarien :",anchor="w").grid(column=0,row=6,columnspan=1,sticky='EW')

        # Entry 

         self.entry1=Tkinter.Entry(self)
         self.entry1.grid(column=1,row=0,sticky='EW',pady=5)
         self.entry2=Tkinter.Entry(self)
         self.entry2.grid(column=1,row=1,sticky='EW',pady=5)
         self.entry3=Tkinter.Entry(self)
         self.entry3.grid(column=1,row=2,sticky='EW',pady=5)
         self.entry4=Tkinter.Entry(self)
         self.entry4.grid(column=1,row=3,sticky='EW',pady=5)
         self.entry5=Tkinter.Entry(self)
         self.entry5.grid(column=1,row=4,sticky='EW',pady=5)
         self.entry6=Tkinter.Entry(self)
         self.entry6.grid(column=1,row=5,sticky='EW',pady=5)


        # Checkbutton
         global CheckVar1
         global CheckVar2
         global Checkbutton1
         global Checkbutton2
         CheckVar1= Tkinter.IntVar()
         CheckVar2=Tkinter.IntVar()
         Checkbutton1=Tkinter.Checkbutton(self, text="Alle Szenarien",variable=CheckVar1,command=self.alle).grid(column=1,row=6,pady=10,sticky='W')
         Checkbutton2=Tkinter.Checkbutton(self, text="Ausgewählte",variable=CheckVar2,command=self.selected).grid(column=1,row=6,padx=110,sticky='W')

        # Button

         button1 = Tkinter.Button(self,text=u"Übernehmen",command=self.Uebernehmen)
         button1.grid(column=1,row=8,pady=10,columnspan=1,sticky='W')
         button2 = Tkinter.Button(self,text=u"Reset",command=self.clear_fields)
         button2.grid(column=1,row=8,pady=10,padx=90,sticky='W')

    def OnButtonClick(self):

        global Anzahl_Szenarien1
        global Anfang_Temperatur1
        global Szenario_all
        global Szenario_all1
        global Szenario_selected
        global ProjectRoot1
        global ProjectName1
        global ExperimentName1
        global PlatformName1

        ProjectRoot1=self.entry1.get()
        ProjectName1=self.entry2.get()
        ExperimentName1=self.entry3.get()
        PlatformName1= self.entry4.get()
        Anzahl_Szenarien1 = int(self.entry5.get())
        Anfang_Temperatur1 = int(self.entry6.get())

        print 'Projekt ordner: %s' % ProjectRoot1
        print 'Projekt Name: %s' % ProjectName1
        print 'Experiment Name: %s' % ExperimentName1
        print 'Platform Name: %s' % PlatformName1
        print 'Anzahl Szenarien: %d' % Anzahl_Szenarien1
        print 'Temperatur Kuhlwasser: %d' % Anfang_Temperatur1




    def clear_fields(self):

        self.entry1.delete(0, 'end')
        self.entry2.delete(0, 'end')
        self.entry3.delete(0, 'end')
        self.entry4.delete(0, 'end')
        self.entry5.delete(0, 'end')
        self.entry6.delete(0, 'end')

        if(CheckVar1.get()):
           CheckVar1.set(0)

        if(CheckVar2.get()):
           CheckVar2.set(0)

    def new_data(self,data):
        Szenario_all=data
        self.OnButtonClick()
        print Szenario_all

    def alle(self):


         self.OnButtonClick()
         all=Anzahl_Szenarien1
         all=all+1
         Szenario_all=list(range(1,all))
         print Szenario_all

    def selected(self):

        child_window(self.parent)         

    def Uebernehmen(self):

        master.destroy()

class child_window(Tkinter.Tk):

    def __init__(self,parent):
        self.root = Tkinter.Toplevel(parent)
        label = Tkinter.Label(self.root,text="Bitte geben Sie die Szenarien nummer ein :",anchor="w").grid(column=0,row=0,columnspan=1,sticky='EW')
        self.root.entry1=Tkinter.Entry(self.root)
        self.root.entry1.grid(column=0,row=1,sticky='EW',padx=10)

        self.ok_button = Tkinter.Button(self.root,text=u"OK",command=self.closechild).grid(column=1,row=2,pady=10,columnspan=1,sticky='W')


    def closechild(self):

        Szenario_all1= self.root.entry1.get()
        self.parent.new_data(Szenario_all1) 

        self.root.destroy()



if __name__ == "__main__":

    master = PST_TEG(None)
    master.title('PST TEG')
    w = 400
    h = 275 
    ws = master.winfo_screenwidth() 
    hs = master.winfo_screenheight() 
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    master.geometry('%dx%d+%d+%d' % (w, h, x, y))
    master.mainloop()`
like image 283
Grover Avatar asked Nov 27 '25 21:11

Grover


1 Answers

This is an example to modify parent Window from child window.

In selected() method pass self argument which will be the parent of child window.

def selected(self):
    child_window(self)

You can change child_window as following:

class child_window(Tkinter.Tk):

    def __init__(self,parent):

        #Save parent reference

        self.parent = parent
        self.root = Tkinter.Toplevel(parent)
        label = Tkinter.Label(self.root,text="Bitte geben Sie die Szenarien nummer ein :",anchor="w").grid(column=0,row=0,columnspan=1,sticky='EW')

        #Create your widget as this, simple to read.

        self.entry1 = Tkinter.Entry(self.root)
        self.entry1.grid(column=0,row=1,sticky='EW',padx=10)

        self.ok_button = Tkinter.Button(self.root,text=u"OK",command=self.closechild).grid(column=1,row=2,pady=10,columnspan=1,sticky='W')


    def closechild(self):

        Szenario_all1= self.entry1.get()

        #self.parent.new_data(Szenario_all1)

        #To change parent value of entry1 from here
        #just call insert() method.

        self.parent.entry1.insert(0, Szenario_all1)

        #To delete content of self.parent.entry1 use:
        #self.parent.entry1.delete(0, len(self.parent.entry1.get()))

        self.root.destroy()
like image 191
Kenly Avatar answered Nov 29 '25 10:11

Kenly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!