Seite 1 von 1

Hilfe!

Verfasst: Montag 30. September 2019, 18:49
von D3us3x
Hallo!
Ich wollte mir heute mein Youtube Converter Help Tool GUI "fähig" machen, weshalb ich mit Tkinter begonnen hab.

Der code sieht folgend aus :

from tkinter import *
import os
import subprocess

def comdm4a():
command=os.system('powershell -command youtube-dl -f m4a' + link)

def comdmp4():
command=os.system('powershell -command youtube-dl -f m4a' + link)

def close_window():
window.destroy()

window = Tk()
window.configure(background='black')
window.title("YOUTUBE-DL HELP TOOL MADE BY D3US3X")

Label(window, text='YOUTUBE-DL HELP TOOL MADE BY D3US3X', bg='black', fg='white', font='verdana 15 bold') .grid(row=0, column=0, sticky=W)
Label(window, text='Normal Path : C:\ ', bg='black', fg='white', font='verdana 12') .grid(row=1, column=0, sticky=W)
Label(window, text='Type in your YouTube Link : ', bg='black', fg='white', font='verdana 12') .grid(row=2, column=0, sticky=W)
link = Entry(window, width=80, bg='grey')
link.grid(row=3, column=0, sticky=W)



m4a = Button(window, text='Convert to m4a!', command=comdm4a , width=15) .grid(row=4, column=1, sticky=E)
mp4 = Button(window, text='Convert to mp4!', command=comdmp4 , width=15) .grid(row=5, column=1, sticky=E)
qut = Button(window, text='Quit!', command=close_window , width=5) .grid(row=6, column=1, sticky=E)

Label(window, text='YouTube Converter by D3US3x is a help tool, that makes it', bg='black', fg='white', font='verdana 11') .grid(row=4, column=0, sticky=W)
Label(window, text='easier to download YouTube Videos from the YouTube Server.', bg='black', fg='white', font='verdana 11') .grid(row=5, column=0, sticky=W)
Label(window, text='All right are going to the youtube-dl developers.', bg='black', fg='white', font='verdana 11') .grid(row=6, column=0, sticky=W)



window.mainloop()





Wenn ich das ganze aber mache bekomme ich das hier :

Traceback (most recent call last):
File "C:\Users\osu\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File ".\YOUTUBE-CONVERTER.py", line 6, in comdm4a
command=os.system('powershell -command youtube-dl -f m4a' + link)
TypeError: can only concatenate str (not "Entry") to str

Re: Hilfe!

Verfasst: Montag 30. September 2019, 22:27
von simplesimon
Hallo!
Die Fehlermeldung enthält die Antwort. Ein String kann nicht mit dem Entry-Widget sondern nur mit dem Inhalt dieses Widgets 'addiert'werden.
Die Lösung:

Code: Alles auswählen

def comdm4a():
    command=os.system('powershell -command youtube-dl -f m4a' + link.get())

def comdmp4():
    command=os.system('powershell -command youtube-dl -f m4a' + link.get())

Re: Hilfe!

Verfasst: Dienstag 1. Oktober 2019, 06:17
von Jankie
Deine zwei Funktionen comdm4a() und comdmp4() machen beide das gleiche. Außerdem ist der Methodenname nicht gut gewählt. Er sollte aussagekräftig sein und (wenn möglich) keine Abkürzungen enthalten.

Re: Hilfe!

Verfasst: Dienstag 1. Oktober 2019, 07:34
von sparrow
Was @Jankie sagt plus: Selbst wenn die beiden Funktionen nur das Gleiche tun, weil du die kopiert und vergessen hast anzupassen, gehören die in eine Funktion und bekommen das, was sie unterscheiden sollen, als Parameter übergeben.

Warum importierst du subprocess aber verwendest es nicht. Nimm subprocess.
Zeichenketten sollte man nicht mit + zusammenstückeln.
Warum wird denn die Powershell aufgerufen? Dafür sehe ich keine Notwendigkeit.

Re: Hilfe!

Verfasst: Dienstag 1. Oktober 2019, 08:48
von Sirius3
Keine *-Importe, weil man sich da unkontrolliert Namen in den eigenen Namensraum schaufelt. Bei tkinter wird üblicherweise `import tkinter as tk` benutzt, und alle Namen daraus per tk.xyz angesprochen.

Kein os.system verwenden, dafür gibt es subprocess.run, und dort auch nicht die String-Variante, sondern das Progamm mit seinen Parametern als Liste übergeben. Alles was eine Funktion braucht, muß sie über ihre Parameter bekommen, hier also `link`.

Danke

Verfasst: Dienstag 1. Oktober 2019, 10:43
von D3us3x
Danke an alle, es hat mir sehr geholfen! :^)

Re: Hilfe!

Verfasst: Montag 7. Oktober 2019, 17:22
von __blackjack__
Ungetestet:

Code: Alles auswählen

#!/usr/bin/env python3
from functools import partial
import subprocess
import tkinter as tk


def run_youtube_dl(file_format, link_entry):
    return subprocess.run(["youtube-dl", "-f", file_format, link_entry.get()])


def main():
    window = tk.Tk()
    window.configure(background="black")
    window.title("YOUTUBE-DL HELP TOOL MADE BY D3US3X")

    tk.Label(
        window,
        text="YOUTUBE-DL HELP TOOL MADE BY D3US3X",
        bg="black",
        fg="white",
        font="verdana 15 bold",
    ).grid(row=0, column=0, sticky=tk.W)
    tk.Label(
        window,
        text="Normal Path: C:\\ ",
        bg="black",
        fg="white",
        font="verdana 12",
    ).grid(row=1, column=0, sticky=tk.W)
    tk.Label(
        window,
        text="Type in your YouTube Link: ",
        bg="black",
        fg="white",
        font="verdana 12",
    ).grid(row=2, column=0, sticky=tk.W)
    link_entry = tk.Entry(window, width=80, bg="grey")
    link_entry.grid(row=3, column=0, sticky=tk.W)

    tk.Button(
        window,
        text="Convert to m4a!",
        command=partial(run_youtube_dl, "m4a", link_entry),
        width=15,
    ).grid(row=4, column=1, sticky=tk.E)
    tk.Button(
        window,
        text="Convert to mp4!",
        command=partial(run_youtube_dl, "mp4", link_entry),
        width=15,
    ).grid(row=5, column=1, sticky=tk.E)
    tk.Button(window, text="Quit!", command=window.destroy, width=5).grid(
        row=6, column=1, sticky=tk.E
    )

    tk.Label(
        window,
        text="YouTube Converter by D3US3x is a help tool, that makes it",
        bg="black",
        fg="white",
        font="verdana 11",
    ).grid(row=4, column=0, sticky=tk.W)
    tk.Label(
        window,
        text="easier to download YouTube Videos from the YouTube Server.",
        bg="black",
        fg="white",
        font="verdana 11",
    ).grid(row=5, column=0, sticky=tk.W)
    tk.Label(
        window,
        text="All right are going to the youtube-dl developers.",
        bg="black",
        fg="white",
        font="verdana 11",
    ).grid(row=6, column=0, sticky=tk.W)

    window.mainloop()


if __name__ == "__main__":
    main()