简体   繁体   English

每次在 Entry 文本值中移动<keyrealase>事件发生</keyrealase>

[英]Shift in the Entry text value each time the <KeyRealase> event take places

I created an Entry to select an xlsx file from keyboard or file browser.我从键盘或文件浏览器创建了一个 select 的 xlsx 文件条目。

If this one has an xlsx extension and that it exists in the file explorer so I color it in black, otherwise in red.如果这个有 xlsx 扩展名并且它存在于文件资源管理器中,那么我将它涂成黑色,否则涂成红色。

check_entries allows me to check the file each time a key is pressed when the Entry has focus. check_entries 允许我在 Entry 具有焦点时每次按下一个键时检查文件。

self.entries['entry1'] = Entry(inputs_frame, width=90, state=DISABLED)

self.entries['entry1'].bind("<KeyRelease>", lambda event: self.check_entries(event, self.entries['entry1']))
def check_entries(self, event, entry):

    entry.config(text=event.widget.get())
    all_entry_compliant = True

    for key, entry in self.entries.items():
        entry_compliant = True
        if key != 'report_file_pathname_entry':
            if os.path.exists(entry['text']) == False:
                entry_compliant = False
                all_entry_compliant = False

        if os.path.splitext(entry['text'])[1] != '.xlsx':
            entry_compliant = False
            all_entry_compliant = False

        if entry_compliant:
            entry.configure(fg='black')
        else:
            entry.configure(fg='red')

    if all_entry_compliant:
        self.buttons['launch_btn'].configure(state=NORMAL)
    else:
        self.buttons['launch_btn'].configure(state=DISABLED)

I don't understand why this writes the text with an offset.我不明白为什么这会用偏移量写入文本。 I alose tried to use <Key>, <FocusOut> event and to filter pressed key with regular expression.我也尝试使用 <Key>、<FocusOut> 事件并使用正则表达式过滤按下的键。

After selected xlsx file from file browser从文件浏览器中选择 xlsx 文件后

After erase the letter 'o'擦掉字母'o'后

After rewrite the letter 'o'重写字母“o”后

After press left arrow key without pressed 'o'按左箭头键而不按'o'

#(Edit) Adding of minimable reproducible example #(Edit) 添加最小化可重现示例

import tkinter as tk

def check_entries(event, entry):
    entry.config(text=event.widget.get())

root = tk.Tk()
entries = {}
entries['entry1'] = tk.Entry(root, width=90)
entries['entry1'].pack()

entries['entry1'].bind("<KeyRelease>", lambda event: check_entries(event, entries['entry1']))
root.mainloop() 

If you try this code, when you press any letter or spam arrow on your keyboard, entry's text will take his old value.如果你尝试这段代码,当你按下键盘上的任何字母或垃圾邮件箭头时,条目的文本将采用他的旧值。

When you do entry.config(text=event.widget.get()) , you are not changing the text in the entry widget.当您执行entry.config(text=event.widget.get())时,您并没有更改条目小部件中的文本。 The widget does not have a text attribute.该小部件没有text属性。 When you use text , tkinter interprets that as an abbreviation for textvariable .当您使用text时,tkinter 会将其解释为textvariable的缩写。 So, you are just setting textvariable to the value in the widget.因此,您只是将textvariable设置为小部件中的值。

If you want to modify the entry widget, you can use the delete method to delete the text, and insert to insert new data.如果要修改条目widget,可以使用delete方法删除文本, insert插入新数据。

For example, to replace the contents of entry with the text "hello, world", you can do it like this:例如,要用文本“hello, world”替换entry的内容,您可以这样做:

entry.delete(0, "end")
entry.insert(0, "hello, world")

In your example you're attempting to replace the text with what is already there, so I'm not sure what you're actually wanting to replace it with.在您的示例中,您试图用已经存在的文本替换文本,所以我不确定您实际想要用什么替换它。 It seems pointless to delete and just re-insert the same text.删除并重新插入相同的文本似乎毫无意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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