【问题标题】:unknown string to raw string未知字符串到原始字符串
【发布时间】:2020-06-18 11:43:04
【问题描述】:

所以我有一个浏览按钮,它指示下载路径,但由于它会输出一个常规字符串,因此它不会下载,因为涉及反斜杠并且它们不会被逐字解释。编辑:我添加了一些代码的其他部分,因为有些部分不是很清楚。

def browse():
    global folder_path
    filename = filedialog.askdirectory()
    Path = filename
    print(Path)
BROWSEbutton = tk.Button(src, text="Browse", font="Courier 12", command=browse).place(x=425,y=0)
def Convert():
    try:   
        video = yt.YouTube(URL.get()).streams.first()
        try:
            video.download(Path)
            print("succesful")
        except:
            print("error")
            msgb.showerror("Error","Invalid Path")     
    except:
        print("error")    
        msgb.showerror("Error","Invalid URL")
CONVERTbutton = tk.Button(src, text="Convert", font="Courier 12",command=Convert).place(x=243,y=220)

【问题讨论】:

  • 最好也打印异常消息。
  • 反斜杠的解释不重要。运行代码时会发生什么。你怎么知道他们没有得到妥善处理?
  • 如果您的缩进与您的实际代码相匹配,那么browse() 是一个完全没用的函数——它要求用户选择一个目录,然后无法将用户的选择存储在任何仍然会出现的地方函数返回后存在。

标签: python string tkinter pytube


【解决方案1】:
  1. 您正在定义一个全局变量,例如 folder_path,但您没有使用它
  2. convert() 中的路径未在应该使用全局变量 folder_path 的函数中定义。
  3. filedialog.askdirectory() 给出的路径也适用于video.download()

删除这些错误后,您的代码应该是,

folder_path=""
def browse():
    global folder_path
    folder_path = filedialog.askdirectory()
    print(folder_path)

def Convert():
    global folder_path
    try:   
        video = yt.YouTube(URL.get()).streams.first()
        try:
            video.download(folder_path)
            print("succesful")
        except:
            print("error")
            msgb.showerror("Error","Invalid Path")     
    except:
        print("error")    
        msgb.showerror("Error","Invalid URL")

BROWSEbutton = tk.Button(src, text="Browse", font="Courier 12", command=browse).place(x=425,y=0)
CONVERTbutton = tk.Button(src, text="Convert", font="Courier 12",command=Convert).place(x=243,y=220)

希望对你有帮助!

【讨论】:

猜你喜欢
  • 2018-11-13
  • 1970-01-01
  • 2023-01-10
  • 2020-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
相关资源
最近更新 更多