[英]Check if Python (3.4) Tkinter Entry is Full
I am using a standard tkinter entry to input a directory path. 我正在使用标准的tkinter条目输入目录路径。 When the user presses enter - if the physical length of the string exceeds that of the entry, I want the program to modify the displayed text to ...[end of directory]. 当用户按下Enter键时-如果字符串的物理长度超过了条目的长度,我希望程序将显示的文本修改为... [目录末尾]。 I have the logistics of this figured out but as of yet I have no accurate way to test whether the entry box is full, or how full 我已经解决了这个问题,但是到目前为止 , 我还没有准确的方法来测试输入框是否已满或是否已满
Things I have tried: 我尝试过的事情:
Another compromise behaviour which I tried was to make the entry "look" at the start of the path. 我尝试的另一种折衷行为是使条目“看起来”在路径的开头。 I tried inserting at, selecting at and moving the cursor to position 0 but none of these worked 我尝试插入at,选择at并将光标移动到位置0,但这些都不起作用
You can use xview
method of Entry
. 您可以使用Entry
xview
方法。 xview
return the visible part of the text displayed in the entry. xview
返回条目中显示的文本的可见部分。 You can use it to interactively create a text that fit the entry. 您可以使用它来交互式地创建适合该条目的文本。
Here is a quick and dirty proof of concept 这是一个快速而肮脏的概念证明
from tkinter import *
root = Tk()
v = StringVar(root)
e = Entry(root,textvariable=v)
e.pack(fill=BOTH)
v.set('abcdefghijklmnopqrstuvwxyz0123456789')
new_s = None
def check_length():
global new_s
original_s = v.get()
def shorten():
global new_s
e.xview(0)
if e.xview()[1] != 1.0:
new_s = new_s[:-4] + '...'
v.set(new_s)
print("new_s: " + new_s)
e.xview(0)
e.after(0,shorten)
print(e.xview()[1])
if e.xview() != (0.0,1.0):
new_s = original_s + '...'
shorten()
b = Button(root,text="hop",command=check_length)
b.pack()
e.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.