Tkinter Checkbuttons不改变变量值 [英] Tkinter Checkbuttons not changing the variable value

查看:58
本文介绍了Tkinter Checkbuttons不改变变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Tkinter的技能每天都在提高,我不敢相信自己与2周前相比有多远.

My skills with Tkinter are improving day by day, I cannot believe how far I am compared to 2 weeks ago.

现在,我的问题是我无法使Checkbuttons正常工作.出于某种原因,我始终无法理解它们,因此它们始终保持相同的值,无论是否选中它们都无关紧要.

Now my problem is that I cannot make the Checkbuttons work. For some reason that escapes my mind, they always remain with the same value, it doesn't matter if they are checked or not.

我在另一个脚本中尝试过单独的复选按钮,它们可以正常工作.它们也可以在我的GUI的另一个窗口中工作,但是当我将它们放在应有的窗口中时,它们将停止工作.

I have tried the checkbuttons alone in another script and they work perfectly. They also work in another windows from my GUI, but when I put them on the window they should be in, they just stop working.

您知道为什么会发生这种情况吗?

Do you have any idea why this could be happening?

谢谢!:D

我尝试为检查按钮设置不同的默认值,例如10表示有效,20表示无效,但是变量没有改变,它的值仍然为0.

I tried setting different default values to the check button, like 10 for active and 20 for not active, but the variable does not change, it still has a value of 0.

from tkinter import *


def runp(): 

    def cb(vari):
        print ("variable is {0}".format(vari.get()))


    window = Tk()
    window.title("Please choose the parameters")
    window.geometry('500x350')

    labelSelect=Label(window, text="Which Rdata file would you like to load? (from output directory)")
    labelSelect.grid(column=0, row=11)

    FastaC=BooleanVar()
    RwMatrix=BooleanVar()
    RwSum=BooleanVar()
    RwInfo=BooleanVar()

    FastaCRadio=Checkbutton(window, text="FastaClean.Rdata", variable=FastaC, command=lambda: cb(FastaC))
    FastaCRadio.grid(column=1, row=11)

    RwMatrixRadio=Checkbutton(window, text="RwMatrix.Rdata", variable=RwMatrix, command=lambda: cb(RwInfo))
    RwMatrixRadio.grid(column=1, row=12)
    RwSumRadio=Checkbutton(window, text="RwSum.Rdata", variable=RwSum, command=lambda: cb(RwSum))
    RwSumRadio.grid(column=1, row=13)
    RwInfoRadio=Checkbutton(window, text="RwInfo.Rdata", variable=RwInfo,command=lambda:cb(RwInfo))
    RwInfoRadio.grid(column=1, row=14)

    window.mainloop()

master=Tk()

Button(master, text="RW", command=runp).pack()

master.mainloop()

推荐答案

您需要通过创建顶级实例来创建子窗口:

You need to create child window by creating instances of Toplevel:

window = Tk()更改为 window = Toplevel()

对于在另一个窗口上打开的窗口,需要使用Toplevel().

need to use Toplevel() for a window that opens on another window.

代码:

from tkinter import *


def runp():

    def cb(vari):
        print ("variable is {0}".format(vari.get()))


    window = Toplevel() # <-------------------
    window.title("Please choose the parameters")
    window.geometry('500x350')

    labelSelect=Label(window, text="Which Rdata file would you like to load? (from output directory)")
    labelSelect.grid(column=0, row=11)

    FastaC=BooleanVar()
    RwMatrix=BooleanVar()
    RwSum=BooleanVar()
    RwInfo=BooleanVar()

    FastaCRadio=Checkbutton(window, text="FastaClean.Rdata", variable=FastaC, command=lambda: cb(FastaC))
    FastaCRadio.grid(column=1, row=11)

    RwSumRadio=Checkbutton(window, text="RwMatrix.Rdata", variable=RwMatrix, command=lambda: cb(RwMatrix))
    RwSumRadio.grid(column=1, row=12)
    RwSumRadio=Checkbutton(window, text="RwSum.Rdata", variable=RwSum, command=lambda: cb(RwSum))
    RwSumRadio.grid(column=1, row=13)
    RwInfoRadio=Checkbutton(window, text="RwInfo.Rdata", variable=RwInfo,command=lambda:cb(RwInfo))
    RwInfoRadio.grid(column=1, row=14)

    window.mainloop()

master=Tk()

Button(master, text="RW", command=runp).pack()

master.mainloop()

这篇关于Tkinter Checkbuttons不改变变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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