[英]Why can't I change my label text in tkinter (Python)?
我正在嘗試在啟動 myintro 時為現有的 label 設置文本。
我收到此錯誤:
infoLabel.config(text = 'This is the intro:') File "C:\Python39\lib\ tkinter_init _.py",第 1646 行,在配置中返回 self。 configure('configure', cnf, kw) 文件“C:\Python39\lib\ tkinter_init .py”,第 1636 行,在 _configure self.tk.call(_flatten((self._w, cmd)) + self._options (cnf)) _tkinter.TclError: 無效的命令名“.!label”
from tkinter import *
#Window properties
root = Tk()
root.title('CIT 144 Final, XXXX XXXX')
root.geometry('400x275')
#===============Functions==================
def myintro():
infoLabel.config(text = 'This is the intro!')
def main():
return
def buttonOneClick():
return
#==============Window widgets definitions=================
infoLabel = Label(root, text = 'x')
inputBox = Entry(root, width = 18)
buttonOne = Button(root, text = '>>', width=5, command=buttonOneClick)
infoLabel.grid(column=0,row=0)
inputBox.grid(column=0,row=2)
buttonOne.grid(column=0,row=3)
root.mainloop()
myintro()
使用 tkinter 的 mainloop 時的問題是它會充當“while”循環,直到 GUI 關閉。 如果你顛倒這兩行:
myintro()
root.mainloop()
window label 將顯示“這是介紹”。
這就是為什么我建議在使用 tkinter 時使用“更新”功能:
from tkinter import *
#Window properties
root = Tk()
root.title('CIT 144 Final, XXXX XXXX')
root.geometry('400x275')
running = True
#===============Functions==================
def myintro():
infoLabel.config(text = 'This is the intro!')
root.update()
root.update_idletasks()
def main():
return
def buttonOneClick():
return
#==============Window widgets definitions=================
infoLabel = Label(root, text = 'x')
inputBox = Entry(root, width = 18)
buttonOne = Button(root, text = '>>', width=5, command=myintro)
infoLabel.grid(column=0,row=0)
inputBox.grid(column=0,row=2)
buttonOne.grid(column=0,row=3)
while running:
try:
root.update()
root.update_idletasks()
except:
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.