让表情符号在tkinter中工作,并使其可被代码覆盖,但在运行时不能被最终用户覆盖

时间:2019-07-16 10:48:25

标签: python python-3.x tkinter unicode emoji

我需要在tkinter中创建一个存储表情符号的字段,但是当有人按下按钮时,该表情符号将被覆盖。我无法在tkinter中使用表情符号,而且不确定如何覆盖它。

let r1 = &mut name;
fun1(r1);
let r2 = &mut name;
fun2(r2);

字段中的预期in输出,并在按下按钮时变为to。还有一种比文本框更好的方法,可以使字段固定,而不是由最终用户编辑。

错误:import tkinter as tk self.option4 = tk.Button(self, width=10) self.option4["text"] = "no" self.option4["command"] = self.wrong self.option4.pack(side="top") corecalc = "?" self.answercheck = tk.Text(self, height=1, width=5) self.answercheck.pack() self.answercheck.insert(tk.END, corecalc) self.QUIT = tk.Button(self, text="Quit", fg="red", command=root.destroy) self.QUIT.pack(side="bottom") def correct(self): corecalc = "✅" def wrong(self): corecalc = "❌"

1 个答案:

答案 0 :(得分:0)

您可以使用任何tkinter小部件来显示字符-表情符号或其他,如果需要显示单个字符,则可以使用Text小部件。

如果您确实要使用Text,可以通过将其state的键设置为“ disabled”(默认为“ normal”)来将其更改为不可编辑:

self.answercheck = tk.Text(self, height=1, width=5, state="disabled")

一个文本框将要求您在插入新字符之前删除先前的文本,如果只希望对字符进行编程更改,则可以简单地使用tkinter.Label小部件:

import tkinter as tk

w = tk.Tk()
display = tk.Label(w, text="□")
display.pack()

def correct():
  display["text"] = "✅"

def wrong():
  display["text"] = "❌"


button = tk.Button(w, text="no", command=wrong)
button.pack()
button = tk.Button(w, text="yes", command=correct)
button.pack()
tkinter.mainloop()

(在我的构建中-Linux fedora上的Python 3.7,tkinter无法直接处理您的?字符,因为它的代码点位于\ uffff之上)