是否有一種自動方法來識別多個選中的復選框?我不想手動創建多個選擇組合。我想得到:
- 1/3 復選框:如果我單獨選擇第一個或第三個框,我會在文本框中列印“ok”
- 2/3 復選框:如果我同時選擇第一個和第三個,我會在文本框中得到列印“ok”。但是只列印了一個“ok”,而不是兩個 ok 為“ok ok”(所以兩個復選框對應一個 ok)
- 我不想在文本框中列印第二個復選框,因為我故意在上面寫了一個錯誤。
問題是這個功能。可能 AND(或 OR)不是我需要的,我猜,我不知道:
def aaa():
if Checkbutton1.get() and Checkbutton2.get() and Checkbutton3.get():
textbox.insert("end", "Ok")
#I also tried to write: if Button1_func() and Button2_func() and Button3_func():
我想解決方案是為每種型別的多選案例創建其他條件,但我認為有一種更自動和更好的方法,無需創建帶條件的選擇案例,因為我將不得不添加幾十個其他復選框,情況會變得復雜的。
重要提示:請不要對 True 和 False 進行諷刺,因為 5 3 = 8 只是一個簡單的示例,以了解我必須應用其他不同代碼的復選框的邏輯。保持原樣回傳 True 和 False,而不更改函式。
任何人都可以通過向我展示正確的代碼來幫助我嗎?謝謝
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("600x600")
root.configure(bg='white')
Checkbutton1 = IntVar()
Checkbutton2 = IntVar()
Checkbutton3 = 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
#CHECKBOX
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=36)
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=66)
Button3 = Checkbutton(root, text = "Checkbox 3", variable = Checkbutton3, onvalue = 1, offvalue = 0, height = 1,
bg="white", foreground='black', activebackground="white", command=Button3_func)
Button3.place(x=10, y=146)
#BUTTON FUNCTION
def aaa():
if Checkbutton1.get() and Checkbutton2.get() and Checkbutton3.get():
textbox.insert("end", "Ok")
#I also tried to write: if Button1_func () and Button2_func () and Button3_func ():
#TEXTOBOX
textbox = tk.Text(root, width=33, height=10, font=('helvetic', 12))
textbox.place(x=30, y=220)
#BUTTON
button = tk.Button(root, text="Ok", command= lambda: [aaa()])
button.pack()
root.mainloop()
更新在代碼中還有這個我保存和加載復選框:
chk_lst = []
chk_lst.extend([Checkbutton1,Checkbutton2, Checkbutton3])
uj5u.com熱心網友回復:
您可以使用集合來存盤基于這些檢查按鈕狀態的函式參考。然后 insideaaa()執行集合中的所有函式,并確定是否將 OK 放入文本框中:
def clicked(flag, func):
if flag:
funclist.add(func)
else:
funclist.remove(func)
funclist = set()
#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)
#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")
uj5u.com熱心網友回復:
為澄清問題而編輯
因為每個Checkbutton都有一個與之關聯的獨特功能,所以您需要手動定義這些功能。現在,我已經替換了函式來代替。
您實際上不需要將函式傳遞給您的復選按鈕,因為它們僅在您單擊Button. 相反,您可以進行更改aaa,以便它為勾選的函式創建一個函式結果串列Checkbuttons。如果其中任何一個回傳False,請不要列印“OK”。但如果他們都回傳True,列印“OK”
import tkinter as tk
root = tk.Tk()
root.geometry("600x600")
# How many checkboxes you want
buttons = 5
button_eval = [0 for i in range(buttons)]
# If your functions are unique for each checkbox, you will need to define them here
def foo():
return True
def bar():
return False
my_functions = [foo, bar, foo, bar, foo] # You need to have 5 functions in this case for this to work
# Creating variables, buttons etc
x = [10, 10, 10, 10, 10]
y = [36, 66, 96, 126, 156]
checkbuttons_vars = [tk.IntVar() for _ in range(buttons)]
checkbuttons = [tk.Checkbutton(root, text=f"Checkbox {i 1}", variable=checkbuttons_vars[i], onvalue=1, offvalue=0) for i in range(buttons)]
for i, box in enumerate(checkbuttons):
box.place(x=x[i],y=y[i])
#BUTTON FUNCTION
def aaa():
function_results = [function() for i, function in enumerate(my_functions) if checkbuttons_vars[i].get()]
textbox.delete(1.0, "end")
if False not in function_results:
textbox.insert(1.0, "Ok")
#TEXTOBOX
textbox = tk.Text(root, width=33, height=10, font=('helvetic', 12))
textbox.pack()
#BUTTON
button = tk.Button(root, text="Ok", command=aaa)
button.pack()
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/425914.html
