Python - Tkinter CheckButton 问题 [英] Python - Tkinter CheckButton Issue

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

问题描述

这是我的 GUI 程序的一部分,我遇到了问题,希望有人能帮助我.我想要做的是,当您单击检查按钮时,它应该在文本小部件上显示价格,但是当我单击检查按钮时,它给了我一个错误:

This is a part of my GUI program that I'm having a problem with and hoping someone could help me. What I'm trying to do is, when you click the check button, it should show the price on the text widget but when I click the check button it gives me an error:

文件E:\Phython\Theater.py",第 147 行,在 update_text 中如果 self.matinee_price.get():AttributeError: 'Checkbutton' 对象没有属性 'get'

File "E:\Phython\Theater.py", line 147, in update_text if self.matinee_price.get(): AttributeError: 'Checkbutton' object has no attribute 'get'

def matinee_pricing(self):
    #Purchase Label
    self.theater_label = tkinter.Label(text='Purchase Theater Seats Here', font=('Verdana', 15, 'bold'))
    self.theater_label.grid(row=2, column=10)
    #Checkbutton
    self.matinee_price = BooleanVar()
    self.matinee_price = tkinter.Checkbutton(text = '101 \nthru \n105', font=('Verdana', 10), bg='light pink', height=5, width=10,\
                                             variable = self.matinee_price, command = self.update_text)
    self.matinee_price.grid(row=5, column=9)

    self.result = tkinter.Text(width=10, height=1, wrap = WORD)
    self.result.grid(row=20, column=10)

def update_text(self):
    price = ''

    if self.matinee_price.get():
        price += '$50'

    self.result.delete(0.0, END)
    self.result.insert(0.0, price)

推荐答案

您正在用复选框本身覆盖布尔变量.BoolenaVar 的声明需要不同的名称,而您的其他函数需要检查该名称.

You're overwriting the boolean variable with the checkbox itself. The declaration of your BoolenaVar needs a different name, and your other function needs to check that name.

此外,复选框默认使用 0 和 1 来描述它们的状态.如果您想使用布尔值,您需要相应地更改 onvalueoffvalue.

Also, checkboxes use 0 and 1 by default to describe their state. If you want to use a boolean you need to change onvalue and offvalue accordingly.

def matinee_pricing(self):
    # [...]
    self.matinee_price_var = BooleanVar() # Different name for the variable.
    self.matinee_price = tkinter.Checkbutton(text = '101 \nthru \n105', font=('Verdana', 10), bg='light pink', height=5, width=10,\
                                             variable = self.matinee_price_var, onvalue=True, offvalue=False, command = self.update_text)
    # Add onvalue, offvalue to explicitly set boolean states.
    # [...]

def update_text(self):
    price = ''

    # Use the variable, not the checkbox
    if self.matinee_price_var.get(): 
        price += '$50'
    #[...]

P.S.:您不需要 \ 在括号内换行.Python 假定括号内的所有内容都是前面命令的一部分,在这种情况下是复选框.只要确保您没有收到任何语法错误,并且不要跨行拆分单个参数.

P.S.: You don't need the \ to break lines inside a bracket. Python assumes everything inside the brackets is part of the preceeding command, in this case the checkbox. Just make sure you're not getting any syntax errors and don't split individual arguments across lines.

这篇关于Python - Tkinter CheckButton 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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