2016-12-31 10 views
-4

私は簡単な質問があります。私はこの2つの機能を持っていますが、ある関数(Vstredvyp)から別の関数(Vstredvysl)に変数(VstredA1、VstredA2、VstredB1、VstredB2)を取得する方法はわかりません。私はグローバル変数を使用しようとしますが、2番目の関数は変数を読み込めません。関数間の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

2つの 'mainloop()'が 'StringVar()'とウィジェットの値に問題を引き起こすため、 'tkinter'には' mainloop() 'だけが必要です。また、 'Tk()'ウィンドウは1つしかありません。 2番目のウィンドウを作成するには 'Toplevel()'を使います。 – furas

答えて

0

グローバル変数と2つのウィンドウの使用例。

mainloop()Toplevel()の1つで2つ目のウィンドウを作成します。

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() 
関連する問題