python tkinter button args_Python tkinter禁用按钮,直到所有字段都填满

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:

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("", myfunction)

root.mainloop()

解决方案

Tkinter variables have a method called trace to add an observer, so the callback function is called when the value changes. I think it is much more efficient than root.bind("", 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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值