让表情符号在 tkinter 中工作,并使其在运行时可被代码覆盖,但不能被最终用户覆盖
Get emoji working in tkinter and make it overridable by code but not end user during runtime
我需要在 tkinter 中创建一个存储表情符号的字段,但是当有人按下按钮时,该表情符号会被覆盖。我无法在 tkinter 中使用表情符号,我不确定如何覆盖它。
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 = "❌"
字段中的预期输出并在按下按钮时更改为 ❌。还有比文本框更好的方法,使字段固定而不是最终用户可编辑。
错误:_tkinter.TclError: character U+1f532 is above the range (U+0000-U+FFFF) allowed by Tcl
你可以使用任何 tkinter 小部件来显示字符 - 表情符号或其他你需要的 - 如果你想显示单个字符,Text
小部件是一个糟糕的选择,尽管可能。
如果你真的想使用 Text
,你可以通过将它的 state
Key 设置为 "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()
(在我这里的构建中 - Python 3.7 在 Linux fedora 上,tkinter 不能直接处理你的角色,因为它的代码点在 \uffff 之上)
我需要在 tkinter 中创建一个存储表情符号的字段,但是当有人按下按钮时,该表情符号会被覆盖。我无法在 tkinter 中使用表情符号,我不确定如何覆盖它。
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 = "❌"
字段中的预期输出并在按下按钮时更改为 ❌。还有比文本框更好的方法,使字段固定而不是最终用户可编辑。
错误:_tkinter.TclError: character U+1f532 is above the range (U+0000-U+FFFF) allowed by Tcl
你可以使用任何 tkinter 小部件来显示字符 - 表情符号或其他你需要的 - 如果你想显示单个字符,Text
小部件是一个糟糕的选择,尽管可能。
如果你真的想使用 Text
,你可以通过将它的 state
Key 设置为 "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()
(在我这里的构建中 - Python 3.7 在 Linux fedora 上,tkinter 不能直接处理你的角色,因为它的代码点在 \uffff 之上)