我有一個程式,它基于一個條目創建自生成的按鈕,因為這些按鈕沒有存盤在程式中的任何地方,所以在GUI重新啟動后就會消失。我需要這些按鈕在GUI終止后繼續存在,但在每次呼叫提交函式時仍能自我生成。值得注意的是,在完整的程式中,有一個資料庫存盤著f_name。我不確定如何從我的資料庫中為每個資料集拉出f_name,或者我是否可以創建一個函式,在這些按鈕最初生成時將其寫入程式中。
from tkinter import *
root = Tk()
root.title('Button')
root.geometry('400x400')
#Entry & Label[/span]。
f_name = Entry(root, width=30)
f_name.grid(row=0, column=1)
f_name_lbl = Label(root, text="First Name:")
f_name_lbl.grid(row=0, column=0)
#檢索f_name并在下一個最高行生成一個按鈕。
def gen_button()。
auto_button = Button(button_frame, text=f_name.get())
auto_button.pack(side="top")
submit_btn = Button(root, text="提交:", command=gen_button)
submit_btn.grid(row=1, column=0, columnspan=2, ipadx=100)
button_frame = Frame(root)
button_frame.grid(row=2, column=0, columnspan=2, sticky="nsew")
#Credit to Bryan Oakley for the auto_button.pack and the button_frame。
uj5u.com熱心網友回復:
在tkinter或python中沒有任何東西可以為你自動保存和恢復資料,但保存和恢復資料的能力是幾乎所有編程語言的一個基本屬性。你只需要撰寫代碼來完成它。
從現有資料中創建按鈕
讓我們從一個可以接收一個或多個字串并創建按鈕的函式開始。你已經有一個這樣的函式了,但是這個函式需要被賦予字串,而不是讓它從一個 widget 中獲取字串。def add_buttons(*items):
for item in items:
auto_button = Button(button_frame, text=item)
auto_button.pack(side="top"/span>)
有了這個,我們現在可以傳入一個字串串列,這些字串將被轉換為按鈕。
add_buttons("button one"/span>, "button two")。
從檔案中讀取按鈕
接下來,我們需要能夠從某種存盤中讀取字串的串列。存盤方法與本答案的目的無關;該函式只需要回傳一個字串串列。在這種情況下,我們只需從一個平面檔案中讀取。這些字串可以很容易地來自資料庫或基于網路的服務。from pathlib import Path
def get_items(path)。
if path.exists()。
with open(path, "r") as f:
data = f.read().strip().splitlines()。
回傳 data
回傳 []
path = Path("butts.txt")
items = get_items(path)
有了這兩個函式,我們就有辦法讀取一個字串串列并將其轉換為一系列的按鈕。只需幾行額外的代碼,我們就有了一個作業程式:
from pathlib import Path
import tkinter as tk
def add_buttons(*items):
for item in items:
auto_button = tk.Button(button_frame, text=item)
auto_button.pack(side="top"/span>)
def get_items(path):
如果path.exists()。
with open(path, "r"/span>) as f:
data = f.read().strip().splitlines()
回傳資料
回傳 []
root = tk.Tk()
button_frame = tk.Frame(root)
button_frame.pack(fill="both"/span>, expand=True, padx=10, pady=10)
path = Path("butts.txt")
items = get_items(path)
add_buttons(*items)
root.mainloop()
如果你手工編輯作業目錄中的 "buttons.txt "檔案,使其包含一行或多行文本,你將為每行文本獲得一個按鈕。 例如,編輯該檔案以包含以下內容:
button one
button two
button three
當你運行你的程式時,你會得到類似這樣的結果:
將按鈕文本保存到一個檔案中
拼圖的最后一塊是能夠將資料保存到一個檔案中。它看起來很像創建按鈕的函式,只是它需要接受一個專案串列并將其寫入檔案。def save_items(path, items)。
with open(path, "w") as f.
f.write("
".join(items))
從Entry widget創建新按鈕
在你的原始代碼中,你希望能夠通過點擊另一個按鈕并從一個條目小部件中獲取文本來創建按鈕。你可以創建一個新的函式,從條目中獲取文本,然后使用其他函式來創建按鈕并保存文本。def submit():
global items
獲取文本 并將其附加到到我們的global串列of按鈕text。
text = entry.get()
items.append(text)
#創建按鈕
add_buttons(text)
# 保存串列 of items
save_items(path, items)
把它放在一起
這里是完整的程式。它缺少錯誤檢查,而且可能不應該依賴全域變數,但重點是說明如何使用檔案來保存和恢復資料。from pathlib import path
import tkinter as tk
def add_buttons(*items)。
for item in items:
auto_button = tk.Button(button_frame, text=item)
auto_button.pack(side="top")
def get_items(path)。
if path.exists():
with open(path, "r") as f.
data = f.read().strip().splitlines()
return data
return [] 。
def save_items(path, items)。
with open(path, "w") as f.
f.write("
".join(items))
def submit()。
global items
# 獲取文本并將其追加到我們的全域按鈕文本串列中。
text = entry.get()
items.append(text)
# 創建按鈕。
add_buttons(text)
#保存專案串列
save_items(path, items)
root = tk.Tk()
button_frame = tk.Frame(root)
entry = tk.entry(root)
submit = tk.Button(root, text="提交", command=submit)
entry.pack(side="top")
submit.pack(side="top")
button_frame.pack(side="top", fill="both", expand=True)
#從檔案中初始化我們的按鈕串列。
path = Path("button.txt")
items = get_items(path)
add_buttons(*items)
root.mainloop()
總結
這并不是解決問題的唯一方法,但它說明了創建函式以讀取資料、保存資料和顯示資料的一般模式。您可以以任何您認為合適的方式重新實作這些函式。轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/330571.html
標籤:
