Search code examples
pythontkinterattributeerrorcustomtkinter

Python customtkinter AttributeError: 'int' object has no attribute '_root'


i just took over a project in python after a year and i wanted to rebuild it with customtkinter using the documentation. Here is the code:

import customtkinter
import tkinter
from pytube import YouTube
from PIL import Image

customtkinter.set_appearance_mode("system")
customtkinter.set_default_color_theme("blue")

app = customtkinter.CTk()
app.geometry("800x760")
app.title("YouTube Unlocked")

logo = customtkinter.CTkImage(dark_image=Image.open("G:\\ytdownloader\\assets\\icon.png"), size=(260,190))
logoDisplay = customtkinter.CTkLabel(app, text="", image=logo)
logoDisplay.pack(padx=10, pady=10)

linkLabel = customtkinter.CTkLabel(app, text="Video link", font=("Arial", 15))
linkLabel.pack(padx=10, pady=10)

linkTextbox = customtkinter.CTkEntry(app, width=400)
linkTextbox.pack(padx=10, pady=10)

radio_var = tkinter.IntVar(0)
radiobutton_1 = customtkinter.CTkRadioButton(app, text="Video", variable=radio_var, value=1)
radiobutton_2 = customtkinter.CTkRadioButton(app, text="Audio", variable=radio_var, value=2)
radiobutton_1.pack(padx=10, pady=10)
radiobutton_2.pack(padx=10, pady=10)

app.mainloop()

But after writing the radio buttons code (always from the documentation) I got this error:

Traceback (most recent call last):
  File "g:\ytdownloader\main.py", line 23, in <module>
    radio_var = tkinter.IntVar(0)
  File "C:\Users\gp_ga\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 564, in __init__
    Variable.__init__(self, master, value, name)
  File "C:\Users\gp_ga\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 372, in __init__
    self._root = master._root()
AttributeError: 'int' object has no attribute '_root'

Is there any way to fix this? Thanks! (by the way if it's a bad english I want to let you know that i'm using google translate)


Solution

  • tkinter.IntVar() already has a default value of 0. The documentation says the following about the arguments that tkinter.IntVar() can take:

    tkinter.IntVar(master=None, value=None, name=None)

    Since master comes first and you provided 0 as the first positional argument, it believes that it is the master argument. What you want to do is specify the argument value. Like so:

    radio_var = tkinter.IntVar(value=0)