2016-12-31 84 views
-4

我有一個簡單的問題。我有這2個函數,但我不知道如何從一個函數(Vstredvyp)到另一個函數(Vstredvysl)獲取變量(VstredA1,VstredA2,VstredB1,VstredB2)。我嘗試使用全局變量,但第二個函數不能讀取變量。函數之間的python變量

def Vstredvyp(): 
    vstredvyp = Tk.Tk() 
    vstredvyp.title("Stred strany - Vypocet") 
    vstredvyp.minsize(300, 240) 
    vstredvyp.maxsize(600, 480) 
    Tk.Label(vstredvyp, text="Zadejte souřadnice bodu:").pack() 
    RvstredA1 = Tk.LabelFrame(vstredvyp, text="X souradnice A") 
    RvstredA1.pack() 
    RvstredA2 = Tk.LabelFrame(vstredvyp, text="Y souradnice A") 
    RvstredA2.pack() 
    RvstredB1 = Tk.LabelFrame(vstredvyp, text="X souradnice B") 
    RvstredB1.pack() 
    RvstredB2 = Tk.LabelFrame(vstredvyp, text="Y souradnice B") 
    RvstredB2.pack() 
    global VstredA1 
    global VstredA2 
    global VstredB1 
    global VstredB2 
    VstredA1 = Tk.Entry(RvstredA1) 
    VstredA1.pack() 
    VstredA2 = Tk.Entry(RvstredA2) 
    VstredA2.pack() 
    VstredB1 = Tk.Entry(RvstredB1) 
    VstredB1.pack() 
    VstredB2 = Tk.Entry(RvstredB2) 
    VstredB2.pack() 
    VstredA1 = Tk.IntType() 
    VstredA2 = Tk.IntType() 
    VstredB1 = Tk.IntType() 
    VstredB2 = Tk.IntType() 
    Tk.Button(vstredvyp, text="Ok", command=Vstredvysl).pack() 
    Tk.Button(vstredvyp, text="Zpet", command=vstredvyp.destroy).pack() 
    vstredvyp.mainloop() 
    return 


def Vstredvysl(): 
    vstredvysl = Tk.Tk() 
    vstredvysl.title("Stred strany - Vysledek") 
    vstredvysl.minsize(150, 120) 
    vstredvysl.maxsize(300, 240) 
    if VstredA1 == VstredB1 and VstredA2 == VstredB2: 
     Tk.Label(vstredvysl, text="Nelze zjistit stred protoze body jsou totozne").pack() 
     Tk.Button(vstredvysl, text="Zpet", command=vstredvysl.destroy).pack() 
     vstredvysl.mainloop() 
    else: 
     Vstred1 = (VstredA1 + VstredB1)/2 
     Vstred2 = (VstredA2 + VstredB2)/2 
     Tk.Label(vstredvysl, text=Vstred1).pack() 
     Tk.Label(vstredvysl, text=Vstred2).pack() 
     Tk.Button(vstredvysl, text="Zpet", command=vstredvysl.destroy).pack() 
     vstredvysl.mainloop() 
    return 
+0

你應該閱讀一些一般的Python教程。你可以從這裏開始(https://docs.python.org/2.7/tutorial/controlflow.html#defining-functions)。另外,請正確格式化你的代碼(那些**做的是什麼?)。 –

+0

創建全局變量,你必須在函數之外創建它們,稍後使用關鍵字'global'內部函數來通知函數使用這些外部變量而不是創建本地變量。 – furas

+0

'tkinter'只需要一個'mainloop()',因爲兩個'mainloop()'在'StringVar()'和小部件中產生了值問題。它應該只有一個'Tk()'窗口。要創建第二個窗口,使用'Toplevel()' – furas

回答

0

帶全局變量和兩個窗口的工作示例。

它只使用一個mainloop()Toplevel()創建第二個窗口。

import tkinter as tk 

# --- functions --- 

def main(): 
    #inform function to use external/global variable 
    global int_var # only if you will use `=` to change value 

    root = tk.Tk() 
    root.title("Root") 

    # change global variable using `=` 
    int_var = tk.IntVar() 

    # use global variable 
    e = tk.Entry(root, textvariable=int_var) 
    e.pack() 

    tk.Button(root, text="Second", command=second).pack(fill='x') 
    tk.Button(root, text="Exit", command=root.destroy).pack(fill='x') 

    root.mainloop() 


def second(): 
    #inform function to use external/global variable 
    #global int_var # only if you will use `=` to change value 

    other = tk.Toplevel() 
    other.title("Other") 

    # use global variable 
    l = tk.Label(other, textvariable=int_var) 
    l.pack() 

    tk.Button(other, text="Exit", command=other.destroy).pack() 

    #other.wait_window() 

# --- start --- 

# create global variable 
int_var = None 

main()