Python TKinter只读文本框不显示内容

huangapple go评论81阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2023年6月13日 12:27:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定