Python Tkinter 输入板文本 [英] Python Tkinter Entry pad text

查看:36
本文介绍了Python Tkinter 输入板文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何填充入口小部件,使其不会在小部件的边框处开始书写?在视觉上,与入口小部件边框有一点空间.

How could I pad the entry widget so it does not start writing right at the border of the widget? Visually, having a little space from the entry widget border.

我的进步:

entry_widget.bind('<FocusIn>', lambda f: entry_widget.insert(0, ' '))

当用户单击小部件时会添加一个空白区域,但此解决方案存在多个问题:

That adds an empty space when the user clicks the widget but there are multiple issue with this solution:

  1. 当点击退出并再次点击时,它会在空白区域或用户填写的任何文本的顶部添加另一个空间.我考虑清除 FocusOut 上的条目小部件,但是这也将清除用户可能编写的所有文本.

  1. When clicking out and clicking back in, it will add another space on top of the empty space or any text the user had filled in. I considered clearing the entry widget on FocusOut but that would also clear all the text that the user might have written.

用户可以删除插入的空格

获取内容时,开头多出空格.虽然这是一个小问题,可以通过删除内容的第一个字符来解决.

when getting the content, there is the additional space at the beginning. Though this is a small problem which can be solved by removing the first character of the content.

可能还有更多我没有考虑到的问题.

And there might be more issues which I did not account for.

我认为我的代码前进的方式很糟糕,因此我问是否有人知道如何正确"填充入口小部件?

I think the way where my code is heading is bad, therefore I am asking if anyone has any idea how to 'properly' pad the entry widget?

推荐答案

我不知道调整 Entry 填充的本机方法,但这里有一种方法可以得到类似的东西.通过给它一个 FLAT 样式使条目的边框不可见,并将条目嵌入作为条目边框的 Frame 中.然后您可以通过调整条目的borderwidth 来指定填充.示例:

I'm not aware a native way of adjusting the Entry's padding, but here's one way to get something like it. Make the entry's border invisible by giving it a FLAT style, and embed the entry in a Frame that acts as the entry's border. Then you can specify the padding by adjusting the entry's borderwidth. Example:

import tkinter as tk

root = tk.Tk()
frame = tk.Frame(root, borderwidth=5, relief=tk.SUNKEN)
frame.pack()
entry = tk.Entry(frame, borderwidth=15, relief=tk.FLAT)
entry.pack()
root.mainloop()

结果:

这篇关于Python Tkinter 输入板文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆