【发布时间】:2021-02-28 20:40:16
【问题描述】:
我已经成功创建了一个翻译应用程序,可以将英语单词翻译成我的母语。我创建了一个在 tkinter GUI 中打印出字典长度的按钮,但该按钮仅在 CMD 中打印结果;如何让它在我创建的输出小部件中打印出结果?下面是代码的摘录:
from tkinter import *
import tkinter. messagebox
root=Tk()
root.geometry('250x200')
root.title("Meta' Translator")
root.configure(background="#35424a")
def clk():
print(len(exlist))
#heading
lab=Label(root,text='Translate English Words to Meta\'',font=('none 11 bold'))
lab.place(x=0,y=2)
#output label
lab=Text(root,width=5,height=1,font=('Times 18'),fg="black")
lab.place(x=59,y=70)
#print button
but=Button(root,padx=3,pady=3,text='Length',command=clk,bg='powder blue',font=('none 10 bold'))
but.place(x=0,y=70)
exlist={
"abdomen":"fɨbûm", "abdomens":"tɨbûm",
"adam's apple":"ɨ̀fɨ̀g ədɔ'", "adam's apples":"tɨ̀fɨ̀g ədɔ'",
"ankle":"ɨgúm ǝwù"
}
root.mainloop()
【问题讨论】:
-
首先,为什么要为标签和文本小部件使用相同的变量名?其次,当您说“我创建的输出小部件”时,您是指标签还是文本小部件。两者都可以作为输出使用。
-
对不起,这是一个错误;推荐“#output 标签”是#output 小部件。我应该为 lab2 下的变量命名。
标签: python dictionary tkinter