如果按下按钮,则更改标签

时间:2017-12-10 19:44:39

标签: python python-3.x tkinter

我需要帮助tkinter。

我的代码:

#Imports
from tkinter import *


#Defs

#Start window
window = Tk()
window.geometry("700x250")

#Frames
F1 = Frame(window)
#F1.geometry("50x50")
#Buttons
B1 = Button(window, text = "Option 1")
B2 = Button(window, text = "Option 2")

#Placing of buttons
B1.pack(side=LEFT, padx=20)
B2.pack(side=RIGHT, padx=20)

#Labels
L1 = Label(window, text = "Dungeon Game")

#Placing of labels
L1.pack()
F1.pack(side=BOTTOM, pady=20)

我已经知道如何使用.configure(text="stuff here")更新代码。但如果按下按钮,我需要帮助,更新标签。我该怎么做?

1 个答案:

答案 0 :(得分:1)

在以下代码中,Button会更改标签的文本。

import tkinter as tk

root = tk.Tk()

lbl = tk.Label(root, text = "Dungeon")
lbl.pack()

def some_callback_func():
    lbl['text'] = "This is the updated text."

#if btn is pressed then go to some_callback_func
btn = tk.Button(root, text="Update", command=some_callback_func)
btn.pack()

root.mainloop()