1

我收到此错误:

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

尝试运行我的脚本时:

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

我可以从错误消息中看到错误发生在我的 zip 函数发生的行中的某处,但我不知道是什么原因造成的。奇怪的是,如果我没有设置updateJson等于command我的 Tkinter 按钮状态的值,而是设置了 ,这个错误就会消失updateJson,它会在按钮对象初始化时调用函数。我也知道错误在说什么,我只是不知道整数来自哪里,以及如何解决这个问题。任何帮助,将不胜感激。

更新:我刚刚发现两个列表的实际压缩不是问题,但是当我引入for循环时,会出现同样的错误。

4

1 回答 1

0

回答关闭此线程,回答“user2357112 支持 Monica”。这个脚本的问题是for text, entry in zip(text, entry)在for循环中字面上使用“entry”,并在按钮实例创建后执行,这意味着如果updateJson在按钮对象初始化期间调用,则不会抛出错误,entry仍然定义为一个列表。但是, for text, entry in zip(text, entry)在启动时执行后,entry现在被定义为列表中的最后一个对象entry,不再是列表entry本身。当用户按下按钮并被updateJson调用时,会抛出一个错误,因为entry它不再是一个列表(我不是 100% 确定错误部分)。

于 2020-05-13T23:00:44.117 回答