1 回答

TA貢獻1752條經驗 獲得超4個贊
您不需要使用 json,因為您使用的不是 JSON 對象而是 Python 字典。
這是您重構的代碼以使用 3 個按鈕填充應用程序;
[更新] 盡管您需要完全重構代碼,因為您的for循環會立即用字典中的所有內容填充剪貼板。
from tkinter import *
from tinydb import TinyDB, Query
db = TinyDB('clipboard.json')
root = Tk()
root.title("CopyNotes")
root.geometry()
mynotes = {
"B1": ["button1label","button1note"],
"B2":["button2label","button2note"],
"B3":["button3label","button3note"]
}
def cp_to_cb_and_db(note, key):
root.clipboard_append(note[key][1])
print('[+] Adding note: {} to clipboard.'.format(note))
db.insert({key: note})
for key in mynotes:
btnz = Button(
root,
text=mynotes[key][0],
font="Helvetica 10 bold",
bg="silver",
command=cp_to_cb_and_db(mynotes, key),
height=2,
width=13).pack(side=TOP, fill=BOTH, expand=YES)
root.mainloop()
添加回答
舉報