Python &引用;只能将str(而不是“int”连接到str;压缩字符串列表和对象列表时
我得到这个错误:Python &引用;只能将str(而不是“int”连接到str;压缩字符串列表和对象列表时,python,for-loop,tkinter,python-zip,Python,For Loop,Tkinter,Python Zip,我得到这个错误: Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Hunter\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "c:\Users\Hunter\Documents
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Hunter\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "c:\Users\Hunter\Documents\Programming\Python Scripts\Scripts\spoolClient\menuScript.py", line 46, in <lambda>
updateJsonButton = Button(preferences, text="Save Preferences", command=lambda: updateJson())
File "c:\Users\Hunter\Documents\Programming\Python Scripts\Scripts\spoolClient\menuScript.py", line 17, in updateJson
for i, j in zip(entryNames, entry):
File "C:\Users\Hunter\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1643, in cget
return self.tk.call(self._w, 'cget', '-' + key)
TypeError: can only concatenate str (not "int") to str
我可以从错误消息中看到,错误发生在我的zip函数发生的行中的某个地方,但我不知道是什么原因造成的。奇怪的是,如果我没有将updateJson
设置为等于Tkinter按钮状态的命令值,而是设置updateJson
,当按钮对象初始化时调用函数,那么这个错误就会消失。我也知道错误在说什么,我只是不知道整数是从哪里来的,以及如何解决这个问题。任何帮助都将不胜感激
更新:我刚刚发现两个列表的实际压缩并不是问题所在,但当我引入for循环时,同样的错误也会发生。要结束此线程,请回答“user2357112支持Monica”。
此脚本中的问题是对于文本,zip中的entry(text,entry)
在for循环中字面上使用“entry”,并在创建按钮实例后执行,这意味着如果在按钮对象初始化期间调用updateJson
,然后不会抛出错误,因为条目仍然定义为列表。但是,在对文本执行之后,zip中的条目(text,entry)
在启动时执行,条目
现在被定义为列表条目
中的最后一个对象,而不再是列表条目
本身。当用户按下按钮并调用updateJson
时,会抛出一个错误,因为entry
不再是列表(我对错误部分不是100%确定)。要结束此线程,请回答“user2357112支持Monica”。
此脚本中的问题是对于文本,zip中的entry(text,entry)
在for循环中字面上使用“entry”,并在创建按钮实例后执行,这意味着如果在按钮对象初始化期间调用updateJson
,然后不会抛出错误,因为条目仍然定义为列表。但是,在对文本执行之后,zip中的条目(text,entry)
在启动时执行,条目
现在被定义为列表条目
中的最后一个对象,而不再是列表条目
本身。当用户按下按钮并调用updateJson
时,会抛出一个错误,因为entry
不再是一个列表(我对错误部分不是100%确定)。您在同一范围内为两件事使用了entry
名称:用于文本,zip中的条目(text,entry)
。不要那样做。现在entry
是错误的。没关系,只是想一想。在为文本调用后,将调用zip中的条目(text,entry)
,在为同一范围内的两个对象使用条目
名称的循环中重新定义条目:为文本,zip中的条目(text,entry)
。不要那样做。现在entry
是错误的。没关系,只是想一想。在for text之后,调用zip中的条目(text,entry)
,在for循环中重新定义条目
from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
from tkinter import filedialog
import qrMaker
import qrReader
import json
settings = {}
#define vars
preferencesSkippedRows = [1, 3, 5, 7, 9, 11]
def openPreferences():
def updateJson():
print("here")
for i, j in zip(entryNames, entry):
print("loopdie")
value = str(j.get())
settings[i]=value
settingsjson = json.dumps(settings)
print(settingsjson)
f = open("preferences.json","w")
f.write(settingsjson)
f.close()
preferences = Tk()
preferences.title("Preferences")
preferences.iconbitmap(qrMaker.getCurrentPath()+'icon.ico')
preferences.geometry('400x600')
topText = Label(preferences, width=30, text="Filament Spool Client Preferences")
cameraText = Label(preferences, width=30, text="Select Camera Instance:")
cameraEntry = Combobox(preferences, width=30, values=qrReader.getCameras())
qrWidthText = Label(preferences, width=30, text="QR Output Width (in.)")
qrWidthEntry = Entry(preferences, width=30)
qrHeightText = Label(preferences, width=30, text="QR Output Height (in.)")
qrHeightEntry = Entry(preferences, width=30)
text = [cameraText, qrWidthText, qrHeightText]
entry = [cameraEntry, qrWidthEntry, qrHeightEntry]
entryNames = ['cameraEntry', 'qrWidthEntry', 'qrHeightEntry']
updateJsonButton = Button(preferences, text="Save Preferences", command=lambda: updateJson())
for i in preferencesSkippedRows:
preferences.grid_rowconfigure(i, minsize=10)
topText.grid(column = 0, row = 0)
row=2
for text, entry in zip(text, entry):
text.grid(column = 0, row = row)
entry.grid(column = 1, row = row)
row+=2
updateJsonButton.grid(column=1, row=row+2)
preferences.mainloop()
openPreferences() #I call script.openPreferences() in my main program but I left this here for debugging purposes