泡菜弄乱了文字

时间:2019-05-16 20:48:32

标签: python-3.x pickle

我制作了一个虚拟通讯录的应用程序(对于我的学校项目,我有1/2个制作了虚拟通讯录)。保存时,此通讯录会询问您的姓名,保存(或加载通讯录文件)后,它会例如以多种方式输出您的姓名。我的名字叫Eoin Mag Ualghairg,但输出的名字是gEoin Mag Ualghair

我对此完全感到困惑,我不知道该怎么解决

请问完整的代码,请原谅我糟糕的代码(上午13点)

appname = "amserlen Glantaf"
import pickle, tkinter, time
from tkinter import Tk
from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
password = "a1Bc6x"
studentname = []
camau = []
homeworkdis=[]
pnote=[]
tnote=[]
master = []
rar = []

fwin = tkinter.Tk()
fwin.title("Login")
class oof(): #Transition between window 1 and 2
    def nwin():
        fwin.destroy()
        home.gui()



class filing():

    def load():
        Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
        filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
        pickleoff = open(filename, "rb")
        master = pickle.load(pickleoff)

        rar = master[0]
        for i in range(len(master[0])):
            studentname.append(rar[i - 1])
        rar = master[1]
        for i in range(len(master[1])):
            camau.append(rar[i - 1])
        rar = master[2]
        for i in range(len(master[2])):
            homeworkdis.append(rar[i -1])
        rar = master[3]
        for i in range(len(master[3])):
            pnote.append(rar[i - 1])
        rar = master[4]
        for i in range(len(master[4])):
            tnote.append(rar[i - 1])
        oof.nwin()
    def newfilemake(): #requires password so that pupils do not create new save files to remove camau.
        saveasfile = tkinter.Tk()
        explain = Label(saveasfile, text = "Please enter your name below:")
        name = Entry(saveasfile)
        explain.pack()
        name.pack()


        def saveasdo():
            studentname = name.get()
            Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
            filename = filedialog.asksaveasfilename() # show an "Open" dialog box and return the path to the selected file
            print(filename)
            master = [studentname, camau, homeworkdis, pnote, tnote]
            picklingon = open(filename, "wb")
            pickle.dump(master, picklingon)
            picklingon.close()
            oof.nwin()
            saveasfile.destroy()
        goyesbut = Button(saveasfile, text = "Save as", command = saveasdo)
        goyesbut.pack()
class home():#Syling second window

    def addparentnote():
        woof = tkinter.Tk()
        woof.title("Add a parentnote")
        insertwow = Text(woof)
        insertwow.pack()
        def yeeyeejuice():
            insertowowrar = gowowoof.get(1.0, END)
            pnote.append(inserttowowrar)
        gowowoof = Button(woof, text = "Add Parentnote", command = home.yeeyeejuice)
        gowowoof.pack()
    def removeparentnote():
        #removes a parentnote
        print("Coming soon")
    def parentnote():
        #open a new window with password and then add/remove parentnote
        woodwaw = tkinter.Tk()
        woodwaw.title("Add/Remove parentnote")
        woodoof = Button(woodwaw, text = 'Add Parentnote', command = home)
    def addteachernote():
        print("Soon")#adds a teachernote
    def removeteachernote():
        print("Soon")#removes a teacher note
    def teachernote():
        print("Soon")#open a new window with password and then add/remove teachernote


    def adiocam():
        camau += 1        


    def gui():
        swin = tkinter.Tk()
        swin.geometry('600x600')
        swin.title('Home')
        name = Label(swin, text = studentname)
        caminfo = Label(swin, text = str(camau))
        homeworkyeet = Listbox(swin)
        for i in range(len(homeworkdis) -1):
            homeworkyeet.insert(i, homeworkdis[i])
        cpnote = Button(swin, text = 'Add/remove Parentnote', command = home.parentnote)
        ctnote = Button(swin, text = 'Add/remove Teachernote', command = home.teachernote)
        addcam = Button(swin, text = 'Add cam', command = home.adiocam)
        name.grid(row = 0, column = 0, sticky = W)
        caminfo.grid(row = 0, column = 0, sticky = E)
        homeworkyeet.grid(row = 10, column = 0)



new = Button(fwin, text = 'New file', command = filing.newfilemake)
new.grid(row = 0, column = 0, sticky = W)
load = Button(fwin, text = 'Load file', command = filing.load)
load.grid(row = 0, column = 10, sticky = E)

我希望: 欧因·马格·乌尔格海格 但我得到: gEoinMagUalghairg

1 个答案:

答案 0 :(得分:2)

欢迎您使用Stack Overflow,并进行一般的编程;-)

程序更改您的姓名的原因是这段代码:

rar = master[0]
for i in range(len(master[0])):
    studentname.append(rar[i - 1])
...

假设master[0]包含名称“ Alice”,因此len(master[0]) == 5。在这种情况下,range返回值0、1、2、3和4。但是访问数据时要减去1。因此,您要添加名称的元素-1、0、1、2和3。在Python中,负索引用于从头开始访问数据。因此'Alice'[-1]返回'e'。这就是为什么首先复制名称的最后一个字母,然后有效地将其移动到名称的开头的原因(例如,“ Alice”变为“ eAlic”)。

因此,如果将rar[i - 1]更改为rar[i],名称的字母将以正确的顺序排列。但是,它仍然存储为字符列表而不是字符串。这可能不是您想要的,所以您将需要更多更改。

注意:您完全不必使用索引和range。在Python中,您可以编写以下代码:

for item in master[0]:
    studentname.append(item)
for item in master[1]:
    camau.append(item)
for item in master[2]:
    homeworkdis.append(item)
for item in master[3]:
    pnote.append(item)
for item in master[4]:
    tnote.append(item)

如果只想加载数据而不是附加数据,则甚至没有必要。如果要创建新列表,只需编写:

studentname, camau, homeworkdis, pnote, tnote = master

要短很多,并且没有名字被改组或更改为列表;-)

最后,为下次遇到此类问题的建议:尝试找出问题以使其更容易理解。因此,对于这个问题,您可以尝试编写一个小型测试应用程序,在其中仅存储一些测试数据,然后尝试读取和打印它(不包含任何tkinter内容)。这使您自己和他人的理解变得更加容易。如果发布Minimal, Reproducible Example,您可能会在Stack Overflow上获得更好的响应。