英文:
Python TKinter read only texbox not showing the content
问题
I'm using TKinter in Pycharm.
这是表单中文本框的代码:
value_close_open_short = round((np.round(Results_short.iloc[level_short].open_low, 3)) * 100, 2)
close_open_short_box = tk.Entry(root, state="readonly")
close_open_short_box.insert(tk.INSERT, value_close_open_short)
close_open_short_box.grid(row=0, column=1)
value_close_open_short存在并且是正确的。
然而,文本框是空的。我做错了什么?
英文:
I'm using TKinter in Pycharm.
this is the code of a text box in a form:
value_close_open_short = round((np.round(Results_short.iloc[level_short].open_low, 3)) * 100, 2)
close_open_short_box = tk.Entry(root, state="readonly")
close_open_short_box.insert(tk.INSERT, value_close_open_short)
close_open_short_box.grid(row=0, column=1)
value_close_open_short exists and is correct.
However, the text box is empty. What am I doing wrong?
答案1
得分: 2
你正在将Entry
对象初始化为readonly
,然后尝试写入它,但小部件会阻止这样做。相反,你应该初始化输入框,插入内容,然后将其设置为只读。
import tkinter as tk
# 占位符数值
value_close_open_short = 10
root = tk.Tk()
close_open_short_box = tk.Entry(root)
close_open_short_box.insert(tk.INSERT, value_close_open_short)
close_open_short_box.config(state="readonly")
close_open_short_box.grid(row=0, column=1)
root.mainloop()
英文:
You are initialising the Entry
object as readonly
and then attempting to write to it, which the widget blocks. Instead, you should initialise the entry, insert into the entry, and then set it to readonly.
import tkinter as tk
#placeholder value
value_close_open_short = 10
root = tk.Tk()
close_open_short_box = tk.Entry(root)
close_open_short_box.insert(tk.INSERT, value_close_open_short)
close_open_short_box.config(state="readonly")
close_open_short_box.grid(row=0, column=1)
root.mainloop()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论