Python 3保存按钮

2024-04-25 23:26:52 发布

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

我有一个按钮,可以创建另一个按钮,但当我关闭程序,我刚才创建的按钮消失。你知道吗

我知道如何使用pickle保存文件中的内容,但是我应该如何编写代码,以便在打开程序时保存按钮并再次创建它们。你知道吗

代码:

def CreateButton():
    global name,clicked,started
    started=True
    name = str(input("Write a Student Name..."))
    print('variable changed...')
    clicked=True

def update():
    global MainWindow,changed,clicked,name
    #global clicked
    if clicked and started:
        #name = input("Write a Student Name: ")
        button_new=needed_but(MainWindow=MainWindow,color="#E6E6E6",text=name)
        clicked=False
        buttonred=False
    MainWindow.after(1*1000,update)

class needed_but:
    def __init__(self,MainWindow,color,text):
        console = Button(MainWindow, text=text,bg=color, command=self.changecolor)
        console.pack()
        self.instance=console

    def changecolor(self):
        buttonred,buttongreen,buttonblue=get_color()
        global clicked,misses_dict
        #clicked=True
        #global buttoncolor
        if buttonred == True:
            self.instance.configure(bg = "#ff0000")
            dun = self.instance.cget('text')
            print(dun)
            if dun in misses_dict:
                misses_dict[('%s' %(dun))] += 1
            else:
                misses_dict[('%s' %(dun))] = 1
            pickle.dump(dictionary, open("%s\SI\SIA.dat" %(path), 'wb'))
            print(misses_dict)
            buttonred = False
        elif buttongreen == True:
            self.instance.configure(bg = "#00ff00")
        elif buttonblue == True:
            self.instance.configure(bg = "#2e9afe")

Tags: instancetextnameselftruedef按钮global
1条回答
网友
1楼 · 发布于 2024-04-25 23:26:52

how should I write the code so that it saves the buttons and creates them again when I open the program.

不能。这些对象实际上是嵌入式tcl解释器中的对象。无法保存解释器的状态。你知道吗

您必须编写代码以某种格式保存有关小部件的所有信息,这样您就可以读回这些信息并重新创建小部件。你知道吗

相关问题 更多 >