为什么我不能在TKinter条目中插入字符串?

2024-07-07 08:10:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python制作一种购物清单类型的程序(3.7,在windows上,使用TKinter),专门为硬件Synth设置而设计。想想PC部件选择器,但对于synth设置

它将用户对每个组件的选择保存在字典中。然后,当用户按下save按钮时,它使用pickle库将字典保存到一个文件中。当他们再次打开应用程序时,它将从文件中加载字典,并将所有输入框的内容设置为字典中的相应值。它工作得很好,但后来我为用户添加了另一个类别,它停止了工作

代码如下:

#import libraries
from tkinter import *
import pickle

win=Tk() #create window
win.title("Synth Planner") #set title
win.configure(bg="#010") #set background
win.geometry("1200x675") #set size

savedict={"seq":"", "seq$":"","synth1":"","synth1$":"","synth2":"","synth2$":""}

def save(): #when save btn pressed
    #save all entries to dict
    savedict["seq"]=txtSeq.get()
    savedict["seq$"]=txtSeqPrice.get()
    savedict["synth1"]=txtSynth1.get()
    savedict["synth1$"]=txtSynth1Price.get()
#this is the line causing the error:
    savedict["synth2"]=txtSynth2.get()
#^
    savedict["synth2$"]=txtSynth2Price.get()

    #save dict to file
    with open("data","wb") as savefile: #open file
        pickle.dump(savedict,savefile) #use pickle to save dict
    
#create components
lblTitle=Label(win,font=("Trebuchet MS",28),text="Synth Planner",bg="#010",fg="#fff") #title
btnSave=Button(win,font=("Trebuchet MS",12),text="Save",command=save) #save button
lblPrice=Label(win,font=("Trebuchet MS",12),text="Price",bg="#010",fg="#fff") #price label
lblSeq=Label(win,font=("Trebuchet MS",12),text="Sequencer",bg="#010",fg="#fff") #sequencer label
txtSeq=Entry(win,font=("Trebuchet MS",12),bg="#fff",fg="#010") #sequencer entry
txtSeqPrice=Entry(win,font=("Trebuchet MS",12),bg="#fff",fg="#010",width=10) #seq price
lblSynth1=Label(win,font=("Trebuchet MS",12),text="Synth",bg="#010",fg="#fff") #synth 1 label
txtSynth1=Entry(win,font=("Trebuchet MS",12),text="Synth",bg="#fff",fg="#010") #synth 1 entry
txtSynth1Price=Entry(win,font=("Trebuchet MS",12),bg="#fff",fg="#010",width=10) #synth 1 price entry
lblSynth2=Label(win,font=("Trebuchet MS",12),text="Synth",bg="#010",fg="#fff") #synth 2 label
txtSynth2=Entry(win,font=("Trebuchet MS",12),text="Synth",bg="#fff",fg="#010") #synth 2 entry
txtSynth2Price=Entry(win,font=("Trebuchet MS",12),bg="#fff",fg="#010",width=10) #synth 2 price entry

#set all text inputs to previous user inputs, by reading pickle file
with open("data","rb") as savefile: #open file
    savedict=pickle.load(savefile) #load file into dict
#set all entries
txtSeq.insert(0,savedict.get("seq"))
txtSeqPrice.insert(0,savedict.get("seq$"))
txtSynth1.insert(0,savedict.get("synth1"))
txtSynth1Price.insert(0,savedict.get("synth1$"))
txtSynth2.insert(0,savedict.get("synth2"))
txtSynth2Price.insert(0,savedict.get("synth2$"))

#place components
lblTitle.place(x=600,y=20,anchor=N)
btnSave.place(x=1150,y=30,anchor=N)
lblPrice.place(x=300,y=50,anchor=N)
lblSeq.place(x=10,y=80)
txtSeq.place(x=90,y=82)
txtSeqPrice.place(x=270,y=82)
lblSynth1.place(x=10,y=120)
txtSynth1.place(x=90,y=122)
txtSynth1Price.place(x=270,y=122)
lblSynth2.place(x=10,y=160)
txtSynth2.place(x=90,y=162)
txtSynth2Price.place(x=270,y=162)

win.mainloop #keep the window open until closed by user

以下是错误消息:

in insert
    self.tk.call(self._w, 'insert', index, string)
_tkinter.TclError: wrong # args: should be ".!entry5 insert index text"

我用来插入文本的方法在上面的几行中工作得很好,它们也做了同样的事情

如果我需要提供任何其他信息,请告诉我。我不经常在这里发帖

谢谢


Tags: textgetfffsaveplacewinmsbg