2016-12-31 1 views
-4

간단한 질문이 있습니다. 나는이 2 가지 기능을 가지고 있지만 한 가지 기능 (Vstredvyp)에서 다른 기능 (Vstredvysl)으로 변수 (VstredA1, VstredA2, VstredB1, VstredB2)를 가져 오는 방법을 모르겠다. 전역 변수를 사용하려고하지만 두 번째 함수는 변수를 읽을 수 없습니다.함수 사이의 파이썬 변수

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

일반적인 파이썬 자습서를 읽어야합니다. [여기] (https://docs.python.org/2.7/tutorial/controlflow.html#defining-functions)를 시작할 수 있습니다. 또한 코드의 형식을 올바르게 지정하십시오 ('**'는 무엇을하고 있습니까?). –

+0

을 사용하여 전역 변수를 작성하려면 함수 외부에서 변수를 작성한 다음 나중에 함수 내부에 키워드'global'을 사용하여 함수를 작성하는 대신 외부 변수를 사용하도록 함수에 알립니다. – furas

+0

'tkinter'는 두 개의'mainloop()'가'StringVar()'와 위젯의 값에 문제를 일으키기 때문에 오직'mainloop()'만 필요합니다. 그리고 오직 하나의'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() 
관련 문제