简体   繁体   English

检查Python(3.4)Tkinter条目是否已满

[英]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: 我尝试过的事情:

  • Using 'PIL.ImageFont.truetype("font.otp",fontsize).size' - cannot calculate the point at which to cut the directory 使用'PIL.ImageFont.truetype(“ font.otp”,fontsize).size'-无法计算剪切目录的位置
  • Simply using the length of the string against the length of the entry- inaccurate as the font I am using (which I don't want to change if possible) varies in length with each character 只是将字符串的长度与条目的长度相对应,因为我使用的字体不准确(如果可能,我不想更改),每个字符的长度各不相同

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM