简体   繁体   中英

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() . I did use variablename = IntVar() too, what I am trying to do is to make if statements dependant on the value of the 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. 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() . I didn't try to run the code you posted because it isn't a 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.

Here's a minimal program that demonstrates that using an IntVar with a Checkbutton does work as intended.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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