2017-04-11 116 views
0

我需要它,所以當我按下按鈕時,它將在2個標籤之間切換,當我設法創建第二條語句時,現在我需要能夠在兩者之間切換。我不知道該怎麼做才能得到這個工作,請幫助在tkInter中按下按鈕時需要幫助開關標籤

def changeLabelText(): 

z = True 
print(z) 
if z == True: 
    print("The current text is", l1['text']) 
    z = False 
    return z 
elif z == False: 
    print("The current text is", l12['text']) 
    z = True 
    return z 
l1['text'] = "Changed Text" 
l12['text'] = "Text" 

b1 = Button(app, text="Change Text", command=changeLabelText) 
l1 = Label(app, text="Text") 
l12 = Label(app, text="New Text") 

回答

1

目前還不清楚你正在使用的原因的多餘的條件,但這裏有一個小例子,你如何可以切換標籤(技術上 - 文本選項作爲您的功能名稱聲明):

try: 
    import tkinter as tk 
except ImportError: 
    import Tkinter as tk 


def changeLabelText(): 
    l1['text'], l2['text'] = l2['text'], l1['text'] 


app = tk.Tk() 

b1 = tk.Button(app, text="Change Text", command=changeLabelText) 
l1 = tk.Label(app, text="Text") 
l2 = tk.Label(app, text="New Text") 

l1.pack() 
l2.pack() 
b1.pack() 

app.mainloop()