简体   繁体   English

在 python 中更改 tkinter 的 label

[英]changing label of tkinter in python

the code might be a little non-standart / messy.代码可能有点不标准/混乱。 it's basically a guess the number game..这基本上是一个猜数字游戏..

Any way, i want to change the label after the user pressing the yes/no button.无论如何,我想在用户按下是/否按钮后更改label i had some ideas but non of them worked well.. please help me:)我有一些想法,但没有一个效果很好..请帮助我:)

currently the label is "start from last time?"目前 label 是“从上次开始?” and i would like to change it to "choose a number between 1 - 99".我想将其更改为“选择 1 - 99 之间的数字”。

label = Label(root, text="start from last time?")
label.pack()

this is the function that activates when the user pressing the button, you can ignore what is written there just add the code:)这是用户按下按钮时激活的 function,您可以忽略那里写的内容,只需添加代码即可:)

def yes():
fd = open("save.txt", "r")
data = fd.read()
data = data.split("|")
global answer
answer = int(data[0])
global attempts
attempts = int(data[1])
fd.close()

btnYes.pack_forget()
btnNo.pack_forget()

entryWindow.pack()
btnCheck.pack()
btnQuit.pack()
btnSave.pack()
root.geometry("500x150")
text.set("You Have {0} attempts Remaining! Good Luck!".format(attempts))

If what you are trying to do is to change the tk.Label text there are two ways to do it.如果您要做的是更改 tk.Label 文本,有两种方法可以做到。

Label.config(text="Whaterver you want")

or或者

Label["text"] = "New Text"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM