python - 如何解决 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 行。我为其他程序使用了相同类型的代码,并且它工作正常。
解决方案
您需要先将数字转换为字符串才能将其添加到字符串中。
print("Hello World" + str(number))
希望对你有所帮助。
推荐阅读
- python - 需要帮助从 pdf 文件中抓取文本
- typescript - Nestjs 模块加载时间过长
- reactjs - 如何将道具传递给链接路由器?
- ubuntu - Ubuntu 在 SSd 中双启动
- php - PHP正则表达式:从开始和结束删除所有字符
- javascript - 在 ReactJS 中创建一个 Todo 列表,使用映射组件来处理映射数据。(类型注释只能在 TypeScript 文件中使用)
- azure - 从 powershell 更新 azure api 入站策略
- python - 使用 yaml 更漂亮的浮动
- excel - 在 Excel 中拆分行并移动到 csv 行
- azure - Azure Vnet 到站点您的 VPN 连接受到影响,因为 S2S VPN 隧道已断开连接