我有一個可執行代碼,可以在資料庫中保存和加載選中或未選中的復選框。這可以正常作業。
每個復選框都有我在這個問題中由善良的用戶 acw1668 討論過的功能(閱讀解決方案)。我想將解決方案應用于我的這個問題的代碼。您可以從解決基于 True 或 False 的多選復選框問題的用戶的回應代碼中獲取線索。我想將鏈接的另一個問題的解決方案應用于我在這個新問題中報告的我的完整代碼,因為此外這個新問題的代碼具有復選框的保存和加載。
我想要的是?我希望當您單擊“列印”按鈕時,根據復選框及其功能的多項選擇,在文本框中列印“確定”。如果復選框根據函式回傳 False,則不會列印“ok”。如果復選框根據函式回傳 True,則它會在文本框中列印“ok”。如果選擇了所有 3 個復選框,則不會列印任何內容,因為其中有一個復選框 2 可以抵抗 False。例如,如果選擇了 True 的復選框 1 和 3,則列印“ok”。
我要添加的代碼(鏈接的另一個問題的代碼)的目的是識別多個選中的多個復選框是:
def clicked(flag, func):
if flag:
funclist.add(func)
else:
funclist.remove(func)
funclist = set()
def aaa():
# if need to clear the text box, uncomment below line
#textbox.delete("1.0", "end")
if funclist and all(func() for func in funclist):
textbox.insert("end", "Ok")
我保存和加載復選框(包括 GUI)的代碼是:
import sqlite3
from tkinter import *
from tkinter import ttk
import tkinter as tk
import tkinter.messagebox
from tkinter import messagebox
root = tk.Tk()
root.geometry("400x500")
root.configure(bg='white')
chk_lst = []
#CHECKBOX
Checkbutton1 = IntVar()
Checkbutton2 = IntVar()
Checkbutton3 = IntVar()
#CHECKBOX FUNCTION
def Button1_func():
if 5 3 == 8:
return True
else:
return False
def Button2_func():
if 5 3 == 7:
return True
else:
return False
def Button3_func():
if 5 3 == 8:
return True
else:
return False
Button1 = Checkbutton(root, text = "Checkbox 1", variable = Checkbutton1, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white", command=Button1_func)
Button1.place(x=10, y=60)
Button2 = Checkbutton(root, text = "Checkbox 2", variable = Checkbutton2, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white", command=Button2_func)
Button2.place(x=10, y=100)
Button3 = Checkbutton(root, text = "Checkbox 2", variable = Checkbutton3, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white", command=Button3_func)
Button3.place(x=10, y=140)
chk_lst.extend([Checkbutton1,Checkbutton2,Checkbutton3])
#SAVE IN DATABASE
def save():
conn = sqlite3.connect("value.db")
c = conn.cursor()
for idx,chk_btn in enumerate(chk_lst,start=1):
c.execute(f'SELECT button1 FROM table1 WHERE id=?',(idx,))
rec = c.fetchall()
if rec:
c.execute("UPDATE table1 SET Button1=? WHERE id=?;", (chk_btn.get(),idx))
else:
c.execute("INSERT INTO table1 VALUES (?,?,?);", (idx,chk_btn.get()))
conn.commit()
conn.close()
messagebox.showinfo("Saved successfully","Saved successfully")
#LOAD WHEN OPEN WINDOWS
def load():
conn = sqlite3.connect("value.db")
c = conn.cursor()
c.execute("SELECT * FROM table1")
vals = c.fetchall()
for val,chk_btn in zip(vals,chk_lst):
chk_btn.set(val[1])
conn.close()
#SAVE BUTTON
save = Button(root, text="Save", bg='#b40909', foreground='white', command= save)
save.place(x=10, y=10)
#PRINT BUTTON
button = tk.Button(root, text="Print", command= lambda: [aaa()])
button.place(x=100, y=10)
#TEXTOBOX
textbox = tk.Text(root, width=33, height=10, font=('helvetic', 12))
textbox.place(x=10, y=220)
load()
root.mainloop()
簡單的資料庫:
CREATE TABLE "table1" (
"id" INTEGER,
"Button1" TEXT,
PRIMARY KEY("id" AUTOINCREMENT)
);
uj5u.com熱心網友回復:
您還需要另一個串列來存盤函式參考:
chk_lst = []
fn_list = []
然后像這樣更新它chk_list:
chk_lst.extend([Checkbutton1, Checkbutton2, Checkbutton3])
fn_lst.extend([Button1_func, Button2_func, Button3_func])
在內部load(),您可以根據從資料庫中檢索到的資料更新集合: funclist
def load():
conn = sqlite3.connect('value.db')
c = conn.cursor()
c.execute('SELECT * FROM table1 ORDER BY id')
vals = c.fetchall()
for val, chk_btn, func in zip(vals, chk_lst, fn_lst):
chk_btn.set(val[1])
if val[1] == '1':
funclist.add(func)
conn.close()
基于您的代碼的完整示例:
import sqlite3
from tkinter import ttk
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.geometry("600x600")
root.configure(bg='white')
chk_lst = []
fn_lst = []
funclist = set()
Checkbutton1 = tk.IntVar()
Checkbutton2 = tk.IntVar()
Checkbutton3 = tk.IntVar()
#CHECKBOX'S FUNCTIONS
def Button1_func():
if 5 3 == 8:
return True
else:
return False
def Button2_func():
if 5 3 == 7:
return True
else:
return False
def Button3_func():
if 5 3 == 8:
return True
else:
return False
def clicked(flag, func):
if flag:
funclist.add(func)
else:
funclist.remove(func)
#CHECKBOX
Button1 = tk.Checkbutton(root, text = "Checkbox 1", variable = Checkbutton1, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white",
command=lambda: clicked(Checkbutton1.get(), Button1_func))
Button1.place(x=10, y=36)
Button2 = tk.Checkbutton(root, text = "Checkbox 2", variable = Checkbutton2, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white",
command=lambda: clicked(Checkbutton2.get(), Button2_func))
Button2.place(x=10, y=66)
Button3 = tk.Checkbutton(root, text = "Checkbox 3", variable = Checkbutton3, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white",
command=lambda: clicked(Checkbutton3.get(), Button3_func))
Button3.place(x=10, y=146)
chk_lst.extend([Checkbutton1, Checkbutton2, Checkbutton3])
fn_lst.extend([Button1_func, Button2_func, Button3_func])
#SAVE IN DATABASE
def save():
conn = sqlite3.connect('value.db')
c = conn.cursor()
for idx, chk_btn in enumerate(chk_lst, start=1):
try:
c.execute('INSERT INTO table1 VALUES (?, ?)', (idx, chk_btn.get()))
except sqlite3.IntegrityError:
c.execute('UPDATE table1 SET Button1 = ? WHERE id = ?', (chk_btn.get(), idx))
conn.commit()
conn.close()
messagebox.showinfo('SAVE', 'Saved successfully')
#LOAD WHEN OPEN WINDOWS
def load():
conn = sqlite3.connect('value.db')
c = conn.cursor()
c.execute('SELECT * FROM table1 ORDER BY id')
vals = c.fetchall()
for val, chk_btn, func in zip(vals, chk_lst, fn_lst):
chk_btn.set(val[1])
if val[1] == '1':
funclist.add(func)
conn.close()
#BUTTON FUNCTION
def aaa():
# if need to clear the text box, uncomment below line
#textbox.delete("1.0", "end")
if funclist and all(func() for func in funclist):
textbox.insert("end", "Ok")
#SAVE BUTTON
save = tk.Button(root, text="Save", bg='#b40909', foreground='white', command= save)
save.place(x=10, y=10)
#TEXTOBOX
textbox = tk.Text(root, width=33, height=10, font=('helvetic', 12))
textbox.place(x=10, y=220)
#PRINT BUTTON
button = tk.Button(root, text="Print", command= lambda: [aaa()])
button.place(x=100, y=10)
load()
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/427109.html
標籤:Python python-3.x tkinter 复选框
上一篇:從檔案(1)呼叫函式。嘗試使用檔案(2)的“條目”輸入中的變數。最終仍然得到檔案(1)變數
下一篇:圖片不斷被洗掉
