Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 电话簿编码出错了_Python_Tkinter - Fatal编程技术网

Python 电话簿编码出错了

Python 电话簿编码出错了,python,tkinter,Python,Tkinter,这是我的代码: 电话簿 至于我得到的错误,我输入了一个数字,比如“336783”,出于某种原因,我仍然设法得到messagebox,知道如何修复它。任何帮助都将不胜感激。您遇到了什么错误?另外,作为提醒,您应该只调用一次.mainloop,这意味着您应该只有一个Tk实例。如果您想要其他窗口,请使用TopLevel而不是TkWelcome来StackOverflow!请阅读。恰当地提问将帮助你得到更好的答案,并帮助其他人在遇到类似问题时理解你的问题。我还没有仔细查看您的代码,但是您需要修复open

这是我的代码:

电话簿
至于我得到的错误,我输入了一个数字,比如“336783”,出于某种原因,我仍然设法得到messagebox,知道如何修复它。任何帮助都将不胜感激。

您遇到了什么错误?另外,作为提醒,您应该只调用一次.mainloop,这意味着您应该只有一个Tk实例。如果您想要其他窗口,请使用TopLevel而不是TkWelcome来StackOverflow!请阅读。恰当地提问将帮助你得到更好的答案,并帮助其他人在遇到类似问题时理解你的问题。我还没有仔细查看您的代码,但是您需要修复
openphonebook
的缩进。正确的缩进在Python中至关重要。为什么函数外有
global
语句?它们需要进入想要修改指定全局变量的函数内部。
def openphonebook():
root.withdraw()
global SaveName, SavePhone

def add_contact():
    SaveName = InputtedName.get()
    SavePhone = InputtedNumber.get()
    try:
        integer_result = int(SavePhone)
    except ValueError:
        tkMessageBox.showinfo("Error", "Not a Valid Number")
    else:
        addressBook = open(username +".c", "a")
        addressBook.write(SaveName + " " + SavePhone + "\n")
        addressBook.close()
        ShowContent.insert(END, SaveName + '     ' + SavePhone)

def delete_contact():
    selection = ShowContent.curselection()
    ShowContent.delete(selection[0])

AddressBook = Tk()
AddressBook.geometry("700x700")
AddressBook.configure(background="#00d6d6")

Inputs = Frame(AddressBook)
Inputs.configure(background="#00d6d6")
Inputs.pack()

Label(Inputs, text="Name:", bg="#00d6d6").grid(row=0, column=0, sticky=W, pady=15)
InputtedName = StringVar()
nameofPerson = Entry(Inputs, textvariable=InputtedName, highlightbackground="#00d6d6")
nameofPerson.grid(row=0, column=1, sticky=W)

Label(Inputs, text="Phone:", bg="#00d6d6").grid(row=4, column=0, sticky=W, pady=15)
InputtedNumber = StringVar()
phoneofPerson = Entry(Inputs, textvariable=InputtedNumber, highlightbackground="#00d6d6")
phoneofPerson.grid(row=4, column=1, sticky=W)

ButtonLayout = Frame(AddressBook)
ButtonLayout.pack()
ButtonLayout.configure(bg="#00d6d6", pady=25)
AddButton = Button(ButtonLayout, text=" Add Contact ", highlightbackground="#00d6d6", command=add_contact)
UpdateButton = Button(ButtonLayout, text="Update Contact", highlightbackground="#00d6d6")
DeleteButton = Button(ButtonLayout, text="Delete Contact", highlightbackground="#00d6d6", command=delete_contact)
AddButton.pack(side=LEFT, padx=3)
UpdateButton.pack(side=LEFT, padx=3)
DeleteButton.pack(side=LEFT, padx=3)
global SaveName, SavePhone

DisplayedDetails = Frame(AddressBook)
DisplayedDetails.pack()
Scrolling = Scrollbar(DisplayedDetails, orient=VERTICAL)
global ShowContent
ShowContent = Listbox(DisplayedDetails, yscrollcommand=Scrolling.set, height=20, width=50, )
Scrolling.config(command=ShowContent.yview)
Scrolling.pack(side=RIGHT, fill=Y)
ShowContent.pack(side=LEFT, fill=BOTH, expand=1)

AddressBook.mainloop()

root.mainloop()