如何在Tkinter中更改按钮的状态?

2024-09-28 05:39:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试学习Tkinter模块的基础知识,例如制作按钮、复选框和其他基本小部件。我想做一个窗口,有两个复选框“音乐”和“视频”,还有一个按钮“继续”,这样按钮只有在“音乐”复选框被选中时才会被启用。在搜索更多关于这个主题的内容时,我会搜索跟踪程序,但是我无法理解函数callme的以下定义(即在争论中使用*)。 我使用的是Ubuntu12.04和Python2.7,我输入的代码如下所示。在

import tkMessageBox
import Tkinter
top = Tkinter.Tk()
CheckVar1 = Tkinter.IntVar()
CheckVar2 = Tkinter.IntVar()
C1 = Tkinter.Checkbutton(top, text = "Music", variable = CheckVar1, \
         onvalue = 1, offvalue = 0, height=5, \
         width = 20)
C2 = Tkinter.Checkbutton(top, text = "Video", variable = CheckVar2, \
         onvalue = 1, offvalue = 0, height=5, \
         width = 20)
def callme(* args):
   if args[2] == 'w':
   b1['state'] = 'normal'    
def exit():
    pass
b1 = Tkinter.Button(top, text = "Proceed", command = exit)
b1['state'] = 'disabled'
C1.pack()
C2.pack()
b1.pack() 
CheckVar1.trace("w", callme) 
CheckVar2.trace("w", callme)
top.mainloop()

另外,有人能告诉我如何向callme函数传递更多的参数。在


Tags: 函数textimport音乐tkintertop按钮pack
1条回答
网友
1楼 · 发布于 2024-09-28 05:39:39

callme应改为:

def callme(*args):
    if CheckVar1.get() or CheckVar2.get():
        b1['state'] = 'normal'
    else:
        b1['state'] = 'disabled'

如何向callme函数传递更多参数

使用lambda

^{pr2}$

相关问题 更多 >

    热门问题