如何从子窗口获取GUI主窗口中的数据?

时间:2015-11-17 08:29:53

标签: python tkinter

我正在尝试学习Python并尝试构建GUI。我面临的问题如下:

在我的代码中,我试图在“Ausgewahlte'选中复选框,选择后打开子窗口并允许用户输入数据。现在我必须在主GUI中告诉我们从子窗口获取数据,或者根据“Alle”之间的选择使用默认数据。或者' Ausgewahlte'。

首先,我无法在主GUI中使用子窗口中定义的数据。

如果你们中的任何人能够提供帮助,那将是很棒的。

期待你的感谢!!!

此致

`

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()`

3 个答案:

答案 0 :(得分:3)

这是从子窗口修改父窗口的示例。

selected()方法传递self参数中,该参数将是子窗口的父级。

def selected(self):
    child_window(self)

您可以按如下方式更改child_window:

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()

答案 1 :(得分:0)

如果我做对了...... 对于这种输入(字符串我猜)你可以使用

tkSimpleDialog.askstring(title, prompt [,options])

创建对话框。此函数返回输入的字符串。

答案 2 :(得分:0)

我会使用“Ausgewahlte”选项的另一个条目。最初,您可以将该条目设置为禁用,以便不能输入任何文本。

self.entry7['state'] = 'normal' 

选中“Ausgewahlte”复选框后,您可以启用该条目,允许用户输入他/她的文本。

self.variableName = ...

另外我会避免使用全局变量,您可以将它们添加到类中:

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="Ausgewahlte :",anchor="w").grid(column=0,row=6,columnspan=1,sticky='EW')

    label = Tkinter.Label(self,text="Szenarien :",anchor="w").grid(column=0,row=7,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)
    self.entry7=Tkinter.Entry(self, state = 'disabled')
    self.entry7.grid(column=1,row=6,sticky='EW',pady=5)


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

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

def OnButtonClick(self):

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

    print 'Projekt ordner: %s' % self.ProjectRoot1
    print 'Projekt Name: %s' % self.ProjectName1
    print 'Experiment Name: %s' % self.ExperimentName1
    print 'Platform Name: %s' % self.PlatformName1
    print 'Anzahl Szenarien: %d' % self.Anzahl_Szenarien1
    print 'Temperatur Kuhlwasser: %d' % self.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')
    self.entry7.delete(0, 'end')
    self.entry7['state'] = 'disabled'

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

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

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

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

def selected(self):
    self.entry7['state'] = 'normal' 

def Uebernehmen(self):
    master.destroy()



if __name__ == "__main__":

master = PST_TEG(None)
master.title('PST TEG')
w = 400
h = 290 
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()

所以最后我会做这样的事情:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post">
  <input type="text" name="name" id="name">
  <div id="adiv"></div>
  <input type="button" value="submit" id="button">
</form>

<script>
$(document).ready(function(){
$('#button').on('click',function(){

if($('#name').val() == ''){
$('#adiv').text("Please enter name!!");
}else{

    $('#adiv').text($('#name').val());
}

})

})

</script>