Python tkinter禁用按钮,直到所有字段都填满

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16687124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:22:32  来源:igfitidea点击:

Python tkinter disable the button until all the fields are filled

pythonwidgettkinterstate

提问by Chris Aung

Let's say I have 2 entry widgets, 1 option menu(drop down list) and 1 button in tkinter. How can i set the button widget state to DISABLED until all 3 widgets are filled by the user?This is what i have currently:

假设我在 tkinter 中有 2 个条目小部件、1 个选项菜单(下拉列表)和 1 个按钮。在用户填写所有 3 个小部件之前,如何将按钮小部件状态设置为禁用?这是我目前拥有的:

import Tkinter as tk

root = tk.Tk()

entry1=tk.Entry(root,width=15).grid(row=1,column=1)
entry2=tk.Entry(root,width=15).grid(row=1,column=2)

choices=('a','b','c')
var=tk.StringVar(root)
option=tk.OptionMenu(root,var,*choices)
option.grid(row=1,column=3)

button=tk.Button(root,text="submit")
button.grid(row=1,column=4)

root.mainloop()

--EDIT--

- 编辑 -

Tried this way, but i don't think this is the correct way to do it.

尝试过这种方式,但我认为这不是正确的方法。

import Tkinter as tk

root = tk.Tk()
def myfunction(event):
    x=var.get()
    y=entry1.get()
    z=entry2.get()
    print len(x),":",len(y),":",len(z)
    if len(y)>0 and len(x)>0 and len(z)>0:
        button.config(state='normal')
    else:
        button.config(state='disabled')
entry1=tk.Entry(root,width=15)
entry1.grid(row=1,column=1)
entry2=tk.Entry(root,width=15)
entry2.grid(row=1,column=2)

choices=('a','b','c')
var=tk.StringVar(root)
option=tk.OptionMenu(root,var,*choices)
option.grid(row=1,column=3)

button=tk.Button(root,text="submit")
button.grid(row=1,column=4)

root.bind("<Enter>", myfunction)
root.mainloop()

采纳答案by A. Rodas

Tkinter variables have a method called traceto add an observer, so the callback function is called when the value changes. I think it is much more efficient than root.bind("<Enter>", myfunction):

Tkinter 变量有一个方法调用trace添加观察者,所以当值改变时会调用回调函数。我认为它比root.bind("<Enter>", myfunction)以下更有效:

import Tkinter as tk

root = tk.Tk()

def myfunction(*args):
    x = var.get()
    y = stringvar1.get()
    z = stringvar2.get()
    if x and y and z:
        button.config(state='normal')
    else:
        button.config(state='disabled')

stringvar1 = tk.StringVar(root)
stringvar2 = tk.StringVar(root)
var = tk.StringVar(root)

stringvar1.trace("w", myfunction)
stringvar2.trace("w", myfunction)
var.trace("w", myfunction)

entry1 = tk.Entry(root, width=15, textvariable=stringvar1)
entry1.grid(row=1,column=1)
entry2 = tk.Entry(root, width=15, textvariable=stringvar2)
entry2.grid(row=1,column=2)

choices = ('a','b','c')
option = tk.OptionMenu(root, var, *choices)
option.grid(row=1,column=3)

button = tk.Button(root,text="submit")
button.grid(row=1, column=4)

root.mainloop()

回答by arjunaskykok

Try this:

尝试这个:

import Tkinter as tk

root = tk.Tk()
def myfunction(*event):
    x=var.get()
    y=entry1.get()
    z=entry2.get()
    print len(x),":",len(y),":",len(z)
    if len(y)>0 and len(x)>0 and len(z)>0:
        button.config(state='normal')
    else:
        button.config(state='disabled')
entry1=tk.Entry(root,width=15)
entry1.grid(row=1,column=1)
entry2=tk.Entry(root,width=15)
entry2.grid(row=1,column=2)

choices=('a','b','c')
var=tk.StringVar(root)
option=tk.OptionMenu(root,var,*choices)
option.grid(row=1,column=3)

button=tk.Button(root,text="submit")
button.grid(row=1,column=4)
button.config(state='disabled')

root.bind_class("Entry","<FocusOut>",myfunction)
var.trace('w', myfunction)
root.mainloop()

回答by hxwalker

What about validateand validatecommandproperties of Entry ?

怎么样validatevalidatecommand输入的属性?

#!/usr/bin/env python3

import tkinter


class App:

    def __init__(self):
        self.root = tkinter.Tk()
        self.variables = {}
        self.entries = {}
        self.vcmd = (self.root.register(self.observer), '%W', '%P')

        self.make_entry('fname')
        self.make_entry('sname')
        self.make_submit_button('Send')

    def make_entry(self, name):
        self.variables[name] = tkinter.StringVar()
        self.entries[name] = tkinter.Entry(
            self.root,
            textvariable=self.variables[name],
            validate='all',
            validatecommand=self.vcmd)
        self.entries[name].pack(side=tkinter.TOP)

    def make_submit_button(self, text):
        self.submit_button = tkinter.Button(
            self.root,
            text=text,
            state=tkinter.DISABLED)
        self.submit_button.pack(side=tkinter.BOTTOM)

    def observer(self, id_, value):
        id_ = int(id_[1:])
        self.update_submit_button_state(entry_exclude=(value and id_ or None))
        return True

    def update_submit_button_state(self, entry_exclude=None):
        if all(
            var.get() 
            for name, var in self.variables.items()
            if id(self.entries[name]) != entry_exclude
        ):
            self.submit_button.config(state=tkinter.NORMAL)
        else:
            self.submit_button.config(state=tkinter.DISABLED)


App().root.mainloop()