0

First things first, Im german (strings in german aswell) and a bloody beginner when it comes to coding, but its fun and I want to improve.

Im currently working on a Youtube downloader GUI using python and tkinter. The cget attribute has worked in the past but I must have changed some little details that made this error occure.

Heres the code:

from tkinter import *
from tkinter import filedialog
from tkinter import font 
from moviepy import *
from moviepy.video.io.VideoFileClip import VideoFileClip
from pytube import YouTube
from tkinter import *
from tkinter.ttk import *
import shutil


 
# functions
def select_path():
        path = filedialog.askdirectory()
        path_text.config(text=path)

def download_video():
    get_link = url_entry.get()
    user_path = path_text.cget("text")
    screen.title("Lädt herunter...")
    mp4_video= YouTube(get_link).streams.get_highest_resolution().download()
    clip = VideoFileClip(mp4_video)
    clip.close()

    shutil.move(mp4_video, user_path)
    screen.title("Download abgeschlossen!")

# head
screen = Tk()
icon = screen.iconbitmap(r'c:\Users\feera\OneDrive\Desktop\Codes\p3\et_logo.ico')
title = screen.title("EvilTube Downloader")
fonts = list(font.families())
fonts.sort()
screen.resizable(False, False)

# create canvas
canvas = Canvas(screen, width=500, height=500, bd=0, highlightthickness=0, relief='ridge')
canvas.pack(fill="both", expand=True)

# bg image
og_bg_img = PhotoImage(file=r"C:/Users/feera/OneDrive/Desktop/Codes/p3/images/bg_texture.png")

# bg position 
dp_bg_img = og_bg_img.subsample(1,1)
canvas.create_image(0, 0, anchor=NW, image=dp_bg_img)

# ETD Logo 
ETD_logo_img = PhotoImage(file=r"C:/Users/feera/OneDrive/Desktop/Codes/p3/images/et_full_logo.png")

# ETD Logo position
logo_img = ETD_logo_img.subsample(5,5)
canvas.create_image(250, 120, image=logo_img)

# flames 
og_flames_img = PhotoImage(file=r"C:/Users/feera/OneDrive/Desktop/Codes/p3/images/flames.png")

# flames position 
dp_flames_img = og_flames_img.subsample(3,3)
canvas.create_image(0, 280, anchor=NW, image=dp_flames_img)

# url input
url_entry = Entry(screen, width=60)

# add url input 
canvas.create_text(250, 240, text="URL einfügen: ", font=("Agency FB", 17), fill="white")
canvas.create_window(250, 280, window=url_entry)

# select filepath
select_path_button = Button(screen, text="Speicherort auswählen", command=select_path)

# add select filepath 
path_text = canvas.create_text(250, 330, text="Wähle einen Speicherort", font=("Agency FB", 16), fill="white") 
canvas.create_window(250, 375, window=select_path_button) 

# download button
download_button = Button(screen, text="Video herunterladen", command=download_video)

# add download button
canvas.create_window(250, 430, window=download_button)

screen.mainloop()


Ive already tried changing the name of some variables like the "path_text" variable. Im permanently looking for solutions on YT or other coding forums but im clueless what to do as I just started coding.

flexbeatz
  • 17
  • 2
  • 2
    `canvas.create_text()` returns the text object id, which is an integer. So `path_text` is an integer, just as the error says. What were you expecting? – John Gordon Jan 09 '23 at 02:17
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jan 09 '23 at 04:00
  • @JohnGordon as I said Im a bloody beginner so Im not able to detect such simple mistakes yet. How can I fix it though? – flexbeatz Jan 09 '23 at 12:23
  • Does this help? In line 8 change this. user_path = path_text.cget("text") to: user_pathcanvas.itemconfig(path_text, get_link) – toyota Supra Jan 09 '23 at 14:04
  • The Error seems to be gone but now this one occures: _tkinter.TclError: unknown option "-" – flexbeatz Jan 09 '23 at 17:56

1 Answers1

0

As the error says, path_text is an integer which is the item ID of the text created by .create_text().

If you want to get the text of the item, use user_path = canvas.itemcget(path_text, "text") instead. Also path_text.config(text=path) inside select_path() is invalid, use canvas.itemconfig(path_text, text=path) instead.

def select_path():
    path = filedialog.askdirectory()
    canvas.itemconfig(path_text, text=path) # set the text

def download_video():
    get_link = url_entry.get()
    user_path = canvas.itemcget(path_text, "text") # get the text
    ...
acw1668
  • 40,144
  • 5
  • 22
  • 34