Hilfe!

Fragen zu Tkinter.
Antworten
D3us3x
User
Beiträge: 4
Registriert: Montag 30. September 2019, 18:45

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
simplesimon
User
Beiträge: 11
Registriert: Samstag 3. Dezember 2016, 21:50

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())
Jankie
User
Beiträge: 592
Registriert: Mittwoch 26. September 2018, 14:06

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.
Benutzeravatar
sparrow
User
Beiträge: 4195
Registriert: Freitag 17. April 2009, 10:28

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.
Sirius3
User
Beiträge: 17754
Registriert: Sonntag 21. Oktober 2012, 17:20

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`.
D3us3x
User
Beiträge: 4
Registriert: Montag 30. September 2019, 18:45

Danke an alle, es hat mir sehr geholfen! :^)
Benutzeravatar
__blackjack__
User
Beiträge: 13111
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

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()
„All religions are the same: religion is basically guilt, with different holidays.” — Cathy Ladman
Antworten