将tkinter按钮参数保存到文件?

发布于 2025-02-08 03:45:10 字数 689 浏览 4 评论 0原文

我正在尝试弄清楚如何使用Pickle保存TKINTER按钮的参数。

到目前为止,我正在考虑将所有参数放在字符串中,保存字符串,然后从字符串中定义按钮的参数?

它根本不起作用,但是看起来像这样:

def tapisone_on():
    tapisonevar = tkinter.StringVar(win,text= "TAPIS #1\nACTIF", bg="green", fg="black", command = tapisone_off)

def tapisone_off():
    tapisonevar = tkinter.StringVar(win,text= "TAPIS #1", bg="grey", fg="red", command = tapisone_on)

tapisonevar = tkinter.StringVar(win,'frameripple, text ="TAPIS #1", font=44, bg="grey", fg="red", command = tapisone_on')
tapisone = tkinter.Button(tapisonevar)
tapisone.place(relx=0.055, rely=0.105, relwidth = 0.2, relheight=0.4, anchor= "nw")

您是否知道该怎么做?

我需要能够从文件上按下面板上按下的按钮。

I am trying to figure out how to save the parameters of a tkinter button by using pickle.

So far I'm thinking of putting all parameters within a string, saving the string and then defining the button's parameters from the string ?

It doesn't work at all, but it kinda looks like this:

def tapisone_on():
    tapisonevar = tkinter.StringVar(win,text= "TAPIS #1\nACTIF", bg="green", fg="black", command = tapisone_off)

def tapisone_off():
    tapisonevar = tkinter.StringVar(win,text= "TAPIS #1", bg="grey", fg="red", command = tapisone_on)

tapisonevar = tkinter.StringVar(win,'frameripple, text ="TAPIS #1", font=44, bg="grey", fg="red", command = tapisone_on')
tapisone = tkinter.Button(tapisonevar)
tapisone.place(relx=0.055, rely=0.105, relwidth = 0.2, relheight=0.4, anchor= "nw")

Do You have any idea how something like that can be done?

I need to be able to load what buttons where pressed down on a panel from a file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

找回味觉 2025-02-15 03:45:10

这是如何使用泡菜按钮选项存储在文件中的示例,然后稍后将其读取以实际创建相应的小部件。它为每个按钮选项分配一个名称,并在字典中为与该名称相关的字典创建一个条目。每组选项本身都由嵌套词典表示。这使得在创建按钮 s时可以轻松指定它们。

请注意,可以扩展此基本想法,还可以存储有关每个按钮的其他信息,例如应该放置在何处。

import pickle
import tkinter as tk

def tapis_one():
    print('tapis_one executed')

def tapis_two():
    print('tapis_two executed')

tapis_btns = {
    'tapis_one': dict(text="TAPIS #1\nACTIF", bg="green", fg="black", command=tapis_one),
    'tapis_two': dict(text="TAPIS #1", bg="grey", fg="red", command=tapis_two)
}

# Save button parameters to file.
with open('buttons.pkl', 'wb') as outp:
    pickle.dump(tapis_btns, outp, -1)


#--- Sample usage of button parameters file.

root = tk.Tk()
root.geometry('300x300')

# Read button parameters file.
with open('buttons.pkl', 'rb') as inp:
    btn_dict = pickle.load(inp)

def create_button(btn_id):
    btn = tk.Button(root, **btn_dict[btn_id])
    btn.place(relx=0.055, rely=0.105, relwidth = 0.2, relheight=0.4, anchor= "nw")

tapis_two = tk.Button(text='Create tapis_two button',
                      command=lambda: create_button('tapis_two'))
tapis_two.pack(side='bottom')
tapis_one = tk.Button(text='Create tapis_one button',
                      command=lambda: create_button('tapis_one'))
tapis_one.pack(side='bottom')


root.mainloop()

Here's an example of how to use pickle to store Button options in a file and read them back later to use to actually create the corresponding widget. It assigns a name to each group of button options and creates an entry in a dictionary for them associated with that name. Each group of options is itself represented by a nested dictionary. This makes it easy to specify them when creating the Buttons later on.

Note that this basic idea could be extended to also store other information about each button, such as where it should be placed.

import pickle
import tkinter as tk

def tapis_one():
    print('tapis_one executed')

def tapis_two():
    print('tapis_two executed')

tapis_btns = {
    'tapis_one': dict(text="TAPIS #1\nACTIF", bg="green", fg="black", command=tapis_one),
    'tapis_two': dict(text="TAPIS #1", bg="grey", fg="red", command=tapis_two)
}

# Save button parameters to file.
with open('buttons.pkl', 'wb') as outp:
    pickle.dump(tapis_btns, outp, -1)


#--- Sample usage of button parameters file.

root = tk.Tk()
root.geometry('300x300')

# Read button parameters file.
with open('buttons.pkl', 'rb') as inp:
    btn_dict = pickle.load(inp)

def create_button(btn_id):
    btn = tk.Button(root, **btn_dict[btn_id])
    btn.place(relx=0.055, rely=0.105, relwidth = 0.2, relheight=0.4, anchor= "nw")

tapis_two = tk.Button(text='Create tapis_two button',
                      command=lambda: create_button('tapis_two'))
tapis_two.pack(side='bottom')
tapis_one = tk.Button(text='Create tapis_one button',
                      command=lambda: create_button('tapis_one'))
tapis_one.pack(side='bottom')


root.mainloop()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文