在tkInter中按下按钮时需要帮助切换标签

时间:2017-04-11 09:08:06

标签: python-3.x 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 个答案:

答案 0 :(得分: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()