首页 > 解决方案 > 如何解决 TypeError:只能将 str(不是“int”)连接到 str [已解决]

问题描述

我正在用python编写一个程序供我个人使用。但我在这里碰壁了。每当我单击下载按钮时,它都会引发此错误:

`Exception in Tkinter callback
Traceback (most recent call last):
return self.func(*args)
File "f:\OG\Python\InstaDownloader.py", line 28, in Download
if(TheChoice == choiceCB[0]):
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1652, in cget
return self.tk.call(self._w, 'cget', '-' + key)
TypeError: can only concatenate str (not "int") to str`

这是我的程序代码:

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import instaloader
from instaloader import Post

instance = instaloader.Instaloader()

root = Tk()
root.title("Insta Downloader")
root.geometry("275x400") # set window
root.columnconfigure(0,weight=1) # set all content in middle

def Location():
    global Folder_Name
    Folder_Name = filedialog.askdirectory()
    if(len(Folder_Name) > 1):
        DebugLabel.config(text=Folder_Name,fg="green")
    else:
        DebugLabel.config(text="Please Choose Folder!",fg="red")

def Login():
    instance.login(user=unLabel.get(),passwd=pwLabel.get())
    DebugLabel.config(text="Logged in Successfully!",fg="green")

def Download():
    TheChoice = choiceCB.get()
    if(TheChoice == choiceCB[0]):
        instance.download_saved_posts(target = Folder_Name)
        DebugLabel.config(text="Downloaded all of the saved posts successfully!",fg="green")

    elif(TheChoice == choiceCB[1]):
        instance.download_profile(profile_name=singlePoint.get(), target = Folder_Name)
        DebugLabel.config(text="Downloaded all of the posts successfully!",fg="green")

    elif(TheChoice == choiceCB[2]):
        post = Post.from_shortcode(instance.context, singlePoint.get())
        instance.download_post(post,target = Folder_Name)
        DebugLabel.config(text="Downloaded the post successfully!",fg="green")

NameLabel = Label(root,text="Insta Downloader",fg="red",font=("jost",20,"bold"))
NameLabel.grid()

SpacingA = Label(root, text="User Name", fg="black",font=("bold"))
SpacingA.grid()

unLabel = Entry(root,fg="black",font=("jost",10))
unLabel.grid()

SpacingB = Label(root, text="Pass Word", fg="black",font=("bold"))
SpacingB.grid()

pwLabel = Entry(root,fg="black",font=("jost",10))
pwLabel.grid()

SpacingC = Label(root, text="")
SpacingC.grid()

loginButton = Button(root,width=10,bg="red",fg="white",text="Login",font=("jost",10,"bold"),command=Login)
loginButton.grid()

SpacingD = Label(root, text="What do you want to download today ?")
SpacingD.grid()

wtd_Choice = ["Saved", "Profile", "Single"]
choiceCB = ttk.Combobox(root,values=wtd_Choice)
choiceCB.grid()

SpacingE = Label(root, text="Enter the profile or short-code here")
SpacingE.grid()

singlePoint = Entry(root,fg="black",font=("jost",10))
singlePoint.grid()

SpacingF = Label(root, text="")
SpacingF.grid()

pathButton = Button(root,width=10,bg="red",fg="white",text="Choose Path",font=("jost",10,"bold"),command=Location)
pathButton.grid()

SpacingG = Label(root, text="")
SpacingG.grid()

downloadButton = Button(root,width=10,bg="red",fg="white",text="Download",font=("jost",10,"bold"),command=Download)
downloadButton.grid()

SpacingH = Label(root, text="")
SpacingH.grid()

DebugLabel = Label(root,text="Debug Log",fg="blue",font=("jost",10))
DebugLabel.grid()

root.mainloop()

我猜问题出在第 27、32 和 36 行。我为其他程序使用了相同类型的代码,并且它工作正常。

标签: pythontkintercombobox

解决方案


您需要先将数字转换为字符串才能将其添加到字符串中。 print("Hello World" + str(number))

希望对你有所帮助。


推荐阅读