以最佳方式设置入口对象的宽度该字符串完全可见

发布于 2025-02-08 21:08:49 字数 765 浏览 1 评论 0原文

我有以下代码:

from tkinter import *
from tkinter import simpledialog

def resize(lbl):
    USER_INP = simpledialog.askstring(title="User input",
                                  prompt="Please enter your message here:")
    lbl['state'] = 'normal'
    lbl.insert(0, f"Text was \'{USER_INP}\'.")
    lbl['state'] = 'readonly'
    lbl['width'] = 50



top = Tk()
lbl_entry = Entry(top, state="readonly", justify='center', width=10)
lbl_entry.grid(column=0, row=1)


bt_adjust = Button(top, text = "Resize Entry", command=lambda:resize(lbl_entry))
bt_adjust.grid(column=0, row=0)

top.mainloop()

按下按钮bt_adjust 条目>条目对象的宽度自动调整。他们的一种方式是以这种方式动态设置宽度的方式,该文本已在entry元素上完全显示,但是如果宽度较小一点,则文本将不再完全显示?

I have following code:

from tkinter import *
from tkinter import simpledialog

def resize(lbl):
    USER_INP = simpledialog.askstring(title="User input",
                                  prompt="Please enter your message here:")
    lbl['state'] = 'normal'
    lbl.insert(0, f"Text was \'{USER_INP}\'.")
    lbl['state'] = 'readonly'
    lbl['width'] = 50



top = Tk()
lbl_entry = Entry(top, state="readonly", justify='center', width=10)
lbl_entry.grid(column=0, row=1)


bt_adjust = Button(top, text = "Resize Entry", command=lambda:resize(lbl_entry))
bt_adjust.grid(column=0, row=0)

top.mainloop()

After pressing the button bt_adjust the width of the Entry object is adjusting automatically. Is their a way to set the width dynamically in that way the text is fully displayed on the Entry element but if the width would be a little smaller the text wouldn't be fully displayed anymore?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追星践月 2025-02-15 21:08:49

这有帮助吗?通过使用len()来自动调整大小。

按下按钮bt_adjust该条目对象的宽度为
自动调整。

第10行中的代码:

lbl['width'] = f'{len(USER_INP)}

调整大小之前的屏幕截图:

”

调整大小之后:

Does this help? By using len() to adjusting automatically resizing.

After pressing the button bt_adjust the width of the Entry object is
adjusting automatically.

code in line 10:

lbl['width'] = f'{len(USER_INP)}

Screenshot before resizing:

enter image description here

AScreenshot after resizing:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文