无法使用 tkinter 使该功能与复选按钮一起使用 [英] Can't get the function to work with checkbuttons using tkinter

查看:47
本文介绍了无法使用 tkinter 使该功能与复选按钮一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定为什么这不起作用,我试过改变事物的顺序,但它仍然不起作用.代码的重点是显示 Null 一词,当您选择任何复选按钮时,根据它的数量是奇数还是偶数,它会将 Null 一词更改为奇数或偶数.任何帮助都会很好.

I'm not sure why this isn't working, I've tried changing the order of things and stuff but it's still not working. The point of the code is so show the word Null and when you select any of the checkbuttons, depending on if its an odd or even amount, its will change the word Null to Odd or Even. Any help would be good.

# Create a window
the_window = Tk()

# Give the window a title
the_window.title('Odd or Even')


##Some global variables
margin = 8
text = 'Null'

##Changing the size of the window
the_window.geometry("350x150")

##Making the label frame
labelframe = LabelFrame(the_window, bd = 3)
labelframe.grid(columnspan = 3, padx = margin, pady = margin)

##Adding the Label
changeable_label = Label(labelframe, text = text, font = ('Arial', 60),
                           fg = ('black'))
changeable_label.pack(side = TOP)

##Addind the Check Buttons

def odd_even():
    chk_btn_value = value1 + value2 + value3

    if chk_btn_value == 0:
        text = 'Null'
    elif chk_btn_value % 2 != 0:
        text = 'Odd'    
    else:
        text = 'Even'

var1 = IntVar()
var2 = IntVar()
var3 = IntVar()

value1 = var1.get()
value2 = var2.get()
value3 = var3.get()


alpha = Checkbutton(the_window, text = 'Alpha', variable = var1,
                    onvalue = 1, offvalue = 0, command = odd_even)
alpha.grid(row = 1, column = 1)

beta = Checkbutton(the_window, text = 'Beta', variable = var2,
                    onvalue = 1, offvalue = 0, command = odd_even)
beta.grid(row = 1, column = 2)

gamma = Checkbutton(the_window, text = 'Gamma', variable = var3,
                    onvalue = 1, offvalue = 0, command = odd_even)
gamma.grid(row = 1, column = 3)


# Start the event loop
the_window.mainloop()

推荐答案

每次调用函数时都需要获取它们的值,因为每次选中或取消选中它们时,它们都会发生变化.同样要更新标签的文本,您需要使用这样的东西.label_name["text"] = "new_text"

You need to get their values everytime you call the function because they are changing everytime you check or uncheck. Also to update label's text, you need to use something like this. label_name["text"] = "new_text"

除此之外,Checkbutton 的默认开启和关闭值分别为 1 和 0.你不需要写那些.

Additional to that, Checkbutton's default on and off values are 1 and 0 respectively. You don't need to write those.

将所有这些应用到您的代码中,会产生这样的结果.

Applying all these to your code, will result this.

from Tkinter import *

def odd_even():
    value1 = var1.get()
    value2 = var2.get()
    value3 = var3.get()
    chk_btn_value = value1 + value2 + value3

    if chk_btn_value == 0:
        changeable_label['text'] = "Null"
    elif chk_btn_value % 2 != 0:
        changeable_label['text'] = "Odd"
    else:
        changeable_label['text'] = "Even"

# Create a window
the_window = Tk()

# Give the window a title
the_window.title('Odd or Even')


##Some global variables
margin = 8
text = 'Null'

##Changing the size of the window
the_window.geometry("350x150")

##Making the label frame
labelframe = LabelFrame(the_window, bd = 3)
labelframe.grid(columnspan = 3, padx = margin, pady = margin)

##Adding the Label
changeable_label = Label(labelframe, text = text, font = ('Arial', 60),
                           fg = ('black'))
changeable_label.pack(side = TOP)

##Addind the Check Buttons

var1 = IntVar()
var2 = IntVar()
var3 = IntVar()

alpha = Checkbutton(the_window, text = 'Alpha', variable = var1,
                     command = odd_even)
alpha.grid(row = 1, column = 1)

beta = Checkbutton(the_window, text = 'Beta', variable = var2,
                     command = odd_even)
beta.grid(row = 1, column = 2)

gamma = Checkbutton(the_window, text = 'Gamma', variable = var3,
                     command = odd_even)
gamma.grid(row = 1, column = 3)


# Start the event loop
the_window.mainloop()

这篇关于无法使用 tkinter 使该功能与复选按钮一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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