Python 3 保存按钮

标签 python python-3.x button tkinter pickle

我有一个按钮可以创建另一个按钮,但是当我关闭程序时,我刚刚创建的按钮消失了。

我知道如何使用 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")

最佳答案

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

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

您必须编写代码以某种格式保存有关小部件的所有信息,您可以在其中读回并重新创建小部件。

关于Python 3 保存按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49311773/

相关文章:

python - 我在哪里可以找到 `lib-dynload` 中模块的源代码?

python - scikit learn 插补 NaN 以外的值

Javascript 切换按钮来更改其标题

python - 如何使用OPENCV有效地定位和保存具有特定值的像素?

python - 如何在django模型中获取当前用户?

python-3.x - 如何将已弃用的 tf.train.QueueRunners tensorflow 方法转换为将数据导入新的 tf.data.Dataset 方法

python - pycapture2 视频捕获和线程

python - 如何在 tensorflow 中更新张量内的子张量?

.net - 如何在 VB.NET 中创建命令链接按钮(具有多行文本)?

php - 将 Woocommerce 单个产品页面上的添加到购物车按钮替换为产品类别