使用检查按钮禁用窗口小部件? [英] Disable widget with checkbutton?

查看:166
本文介绍了使用检查按钮禁用窗口小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何禁用一个条目使用检查按钮...我有这个,但它不工作(python 2.7.1)...

 #!/ usr / bin / env python2.7 
# - * - 编码:utf-8 - * -

来自Tkinter import *

root = Tk()

class Principal(tk.Tk):
def __init __(self,* args,** kwargs):

foo =
nac =

全局ck1
nac = IntVar()
ck1 = Checkbutton(root,text ='Test',variable = nac ,command = self.naccheck)
ck1.pack()

全局变量
ent = Entry(root,width = 20,background ='white',textvariable = foo, state = DISABLED)
ent.pack()

def naccheck(self):
if nac == 1:
ent.configure(state ='disabled' )
else:
ent.configure(state ='normal')

app = Principal()
root.mainloop()


解决方案

我使用Principal类的foo和nac成员变量

  ... 
self.foo = StringVar()
self.foo.set(test)
self .nac = IntVar()
...

然后在naccheck nac

  def naccheck(self):
if self.nac == 1:
ent.configure (state ='disabled')
self.nac = 0
else:
ent.configure(state ='normal')
self.nac = 1

不要忘记更改ck1的变量= self.nac
和ent的textvariable = self.foo。



此外,您可能想创建ck1和ent成员变量,因为您可能有问题,稍后使用naccheck()引用它们



这些更改在我的Python2.7


上正常工作

How can i disable an entry using a checkbutton... i got this but it doens't work (python 2.7.1)...

#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-

from Tkinter import *

root = Tk()

class Principal(tk.Tk):
    def __init__(self, *args, **kwargs):

        foo = ""    
        nac = ""

        global ck1
        nac = IntVar()      
        ck1 = Checkbutton(root, text='Test',variable=nac, command=self.naccheck)
        ck1.pack()

        global ent
        ent = Entry(root, width = 20, background = 'white', textvariable = foo, state = DISABLED)       
        ent.pack()

    def naccheck(self):
        if nac == 1:
            ent.configure(state='disabled')
        else:
            ent.configure(state='normal')       

app=Principal()
root.mainloop()

解决方案

I made foo and nac member variable of the Principal class

    ...
    self.foo = StringVar()
    self.foo.set("test")
    self.nac = IntVar()
    ...

Then in naccheck() reference self.nac

    def naccheck(self):
        if self.nac == 1:
            ent.configure(state='disabled')
            self.nac = 0
        else:
            ent.configure(state='normal')
            self.nac = 1

Dont forget to change ck1's variable = self.nac and ent's textvariable = self.foo.

Also, you might want to make ck1 and ent member variable, as you might have problems referencing them later with naccheck()

Those changes worked fine on my Python2.7

这篇关于使用检查按钮禁用窗口小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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