如何在没有“提交"的情况下更新条目? Tkinter中的按钮? [英] How can I update Entry without a "submit" button in Tkinter?

查看:92
本文介绍了如何在没有“提交"的情况下更新条目? Tkinter中的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有Entries,其中有一些值是从CFG文件中分配给它们的.我想在实时更新Entry时修改CFG文件,而没有submit按钮.

So I have Entries which have some values assigned to them from a CFG File. I want to modify the CFG file when the Entry is updated, live, without a submit button;

使用<Key>绑定将起作用,但是将仅使用前一个值,而不是当前值,因为按下的最后一个键不作为值,而是作为key-press来考虑.

Using <Key> binding will work but will take only the previous value, not the current one, as the last key pressed is not taken into consideration as a value, but as a key-press.

例如:

class EntryBox:
    def __init__(self, value, option, section, grid_row, master_e):
        self.section = section
        self.option = option
        self.box = Entry(master_e)
        self.box.grid(column=0, row=grid_row)
        self.serial_no = grid_row
        self.box.insert(0, value)
        self.box.bind("<Key>", lambda event: update_cfg(event, self, self.get_value()))

    def get_value(self):
        return self.box.get()


    def update_cfg(evt, entry_box,new_value):
        global config_file
        config_file.set(entry_box.section, entry_box.option, new_value)
        print "Config file modified. "+entry_box.section+" "+entry_box.option+" "+new_value

如果当我单击entry并按6时entry中的值是05R,它将打印Config file modified. CURRENT_MEASUREMENT_EXAMPLE_HP shunt_resistance 05R;否则,将显示Config file modified. CURRENT_MEASUREMENT_EXAMPLE_HP shunt_resistance 05R.当我按7后,它将始终打印Config file modified. CURRENT_MEASUREMENT_EXAMPLE_HP shunt_resistance 0R56,依此类推,总是在后面按一下按键.更改值后,实时更新它的唯一方法是按TABarrow按钮.

If the value in the entry is 05R when I click on the entry and press 6, it will print Config file modified. CURRENT_MEASUREMENT_EXAMPLE_HP shunt_resistance 05R; after I press 7, it will print Config file modified. CURRENT_MEASUREMENT_EXAMPLE_HP shunt_resistance 0R56 and so on, always with one keypress behind. The only way to live update it after the value has been changed is to press the TAB or arrow buttons.

推荐答案

您可以使用

  • FocusOut
  • tabenter
  • KeyRelease
  • FocusOut
  • tab or enter Key
  • KeyRelease

绑定可以实现这一点.

bindings to achieve that.

验证功能也可以提供帮助,因为它们具有可用的先前值和新值.请阅读文档了解有关此问题的更多信息.

Also validation functions can help as they have previous and new values available. Please read the docs for more information on that matter.

这是恕我直言,它是实现检查并提交"功能的最"pythonic"/"tkinter"方式.

It is IMHO the most "pythonic" / "tkinter" way of achieving what is a "check and submit" functionality.

编辑

如OP所述,绑定focusout可能会导致问题,以下是其确实如何工作的示例:

As stated by OP, binding focusout could lead to problems here an example how it does indeed work:

import Tkinter as tk
import sys

def focus_out_event(event):
    print >> sys.stderr, "Focus-Out   event called with args: %s"%event
    print >> sys.stderr, "Entry Widget Content:               %s"%event.widget.get()
def key_release_event(event):
    print >> sys.stderr, "Key-Release event called with args: %s"%event
    print >> sys.stderr, "Entry Widget Content:               %s"%event.widget.get()

if __name__ == "__main__":
    root = tk.Tk()
    entry1 = tk.Entry(root)
    entry1.bind("", key_release_event)
    entry1.bind("", focus_out_event)
    entry1.grid()

    entry2 = tk.Entry(root)
    entry2.bind("", key_release_event)
    entry2.bind("", focus_out_event)
    entry2.grid()

    root.mainloop()

测试: -输入文字("asd")到entry1 -单击进入entry2

Test: - enter text ("asd") to entry1 - click into entry2

输出的最后一行是从更改到屏幕截图(触发focusout的事件)

The last line of output is from changing to screenshot (event that fired a focusout)

这篇关于如何在没有“提交"的情况下更新条目? Tkinter中的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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