简体   繁体   English

我无法使用 .get() 获取我的 Tkinter Checkbutton 变量值

[英]I Cant Get My Tkinter Checkbutton Variable Value Using .get()

So I have been working on a project and recently encountered an issue with my check button.所以我一直在做一个项目,最近遇到了我的复选按钮的问题。 Whenever I try to do variablenname.get() , it never works as it says int value has no attribute get() .每当我尝试执行variablenname.get() ,它都不会起作用,因为它说int value has no attribute get() I did use variablename = IntVar() too, what I am trying to do is to make if statements dependant on the value of the checkbutton.我也使用了variablename = IntVar() ,我想要做的是使 if 语句依赖于 checkbutton 的值。

        var = IntVar()
        BTR = Tk()
        Discounts = Checkbutton(BTR, text="Discount (Students, Under 18s, 65+ and Disability Badge Holders)", font = ("times", 30), bg="#252538", fg = "#99FFFF", variable = var)
        Discounts.place(x=120, y=300)
        PriceTag=Label(BTR, text =("£", Cost), font = ("times", 50), fg = "White", bg = "#252538")
        PriceTag.place(x=1000, y= 450)
        val=Label(BTR, text = Tickets, font = ("times", 22), height = 5, width = 15, bg= "White")
        val.place(x=175, y=550)
        add=Button(BTR, text = "+", font = ("times", 22), height = 5, width = 10, bg = "#B266FF", command = lambda: UpdateAdd(val, BTR, PriceTag, BusTicketPrice, price, var))
        add.place(x=420, y=550)

    def UpdateAdd(val, BTR, PriceTag, BusTicketPrice, price, var):
      var = var.get()
      Tickets = Tickets + 1 
      val.destroy()
      val = Label(BTR, text = Tickets, font = ("times", 22), height = 5, width = 15, bg= "White")
      val.place(x=175, y=550)
      PriceTag.destroy()
      if price == "Peak" and var == 0 :
          BusTicketPrice=BusTicketPrice*1.25
      elif var == 1 and price == "Peak":
          BusTicketPrice=BusTicketPrice
      elif var == 1 and price == "Off-Peak":
          BusTicketPrice=BusTicketPrice*0.75
      elif price == "Off-Peak" and var == 0):
          BusTicketPrice=BusTicketPrice
      Cost = BusTicketPrice * Tickets
      Cost = round(Cost, 3)
      PriceTag=Label(BTR, text =("£", str(Cost), "0"), font = ("times", 50), fg = "White", bg = "#252538")
      PriceTag.place(x=1000, y= 450)

There are several problems with this code.这段代码有几个问题。 Firstly, you should not attempt to create any Tkinter objects, including widgets and IntVars, until after you've created the root window and started the Tcl interpreter with the Tk() call.首先,在创建根窗口并使用Tk()调用启动 Tcl 解释器之前,您不应尝试创建任何 Tkinter 对象,包括小部件和 IntVars。 I'm surprised that your code doesn't print an error message like我很惊讶您的代码没有打印类似的错误消息

AttributeError: 'NoneType' object has no attribute '_root'

because you have var = IntVar() before BTR = Tk() .因为你在BTR = Tk()之前有var = IntVar() BTR = Tk() I didn't try to run the code you posted because it isn't a MCVE .我没有尝试运行您发布的代码,因为它不是MCVE

The main cause of your bug is that in UpdateAdd you do var = var.get() near the start of the function (which gets the integer value from var and then binds that integer object to the name var ), but then a bit further down you have var.get() , and that won't work because now var refers to the integer object, not the IntVar it was originally bound to.您的错误的主要原因是在UpdateAdd您在函数开头附近执行var = var.get() (它从var获取整数值,然后将该整数对象绑定到名称var ),但再进一步下来你有var.get() ,这将不起作用,因为现在var指的是整数对象,而不是它最初绑定到的IntVar

Here's a minimal program that demonstrates that using an IntVar with a Checkbutton does work as intended.这是一个最小的程序,它演示了将 IntVar 与 Checkbutton 一起使用确实按预期工作。

import tkinter as tk

BTR = tk.Tk()
var = tk.IntVar()

title = tk.Label(BTR, text="Purchase Bus or Tram Tickets")
title.pack()

discounts = tk.Checkbutton(BTR, text="Discount", variable=var)
discounts.pack()

test = tk.Button(BTR, text="test", command=lambda: print(var.get()))
test.pack()

BTR.mainloop()

When you press the "test" button, the current state of the Checkbutton will be printed to the console.当您按下“测试”按钮时,Checkbutton 的当前状态将打印到控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM