如何在 tkinter 的函数中打印复选框的总点击数?
How can I print total clicked number of checkboxes within a function in tkinter?
我正在尝试在 class 中创建一个函数
函数,我想计算点击了多少个复选框。
# This function assesses number of clicked items in my class's list
def badper(self):
window=Tk()
window.config(bg='lightgreen')
# total = 0
def show(self):
new_label = Label(window, text=var.get()).pack()
var=IntVar()
def create_check_button(self,i):
var = IntVar()
c=Checkbutton(window,text=self.badcharacter[i],variable=var)
c.pack()
# if c == 1:
# global total
# total += 1
# This loop creates checkboxes in the equivalent of number items of my list
for i in range(6):
create_check_button(self,i)
# This button should show how many items clicked so far
my_button=Button(window,text="show", font=("Arial", 12, "bold"),command=show(self)).pack()
此代码实现了正确的结果,显示的集合总数 Checkbuttons
。
代码中的注释解释了这个简化代码段中的问题。
import tkinter as tk
window = tk.Tk()
window.config(bg='lightgreen')
store_var = []
def show():
total = 0
# This is one way to display results
for a in store_var:
if a.get():
total = total + 1
my_label["text"] = f"Total = {total}"
def create_check_button(i):
var = tk.IntVar()
# no information on badcharacter list supplied
c = tk.Checkbutton(
window, text = i, onvalue = 1, offvalue = 0, variable = var)
c.pack()
# Save each IntVar in list for use by function show()
store_var.append(var)
for i in range(6):
create_check_button(i)
# my_label is referenced in function show() so define my_label separately from pack
my_label = tk.Label(window, text = "Output")
my_label.pack()
# Button is not referenced anywhere so combining declaration and pack management is OK
tk.Button( window, text = "show", font = "Arial 12 bold", command = show).pack()
window.mainloop()
这是关于我的项目的图片
I use stepI button in order to display checkbotton window
Thank you derek I am appreciated but when I clicked the show button The result shows "0"
I used the above answer in the program I saw that I could not change the result the total
remained the same(0). So I tried different approach but this time I could not run checkbutton properly. The store_var
displays either "000000" or "111111" (these numbers 0,1 related with item number in my list(i))
def badper(self):
window=tk.Tk()
window.geometry("300x300")
window.config(bg='lightgreen')
store_var= []
def show():
total = 0
# This is one way to display results
for a in store_var:
if a.get():
total = total + 1
my_label["text"] = f"Total = {total}"
def create_check_button(i):
var = tk.IntVar()
# for i in self.badcharacter:
c = tk.Checkbutton(window, text=i,onvalue=1,offvalue=0,variable=var)
c.pack()
# Save each IntVar in list for use by function show()
store_var.append(var)
for i in self.badcharacter:
create_check_button(i)
# my_label is referenced in function show() so define my_label separately from pack
my_label = tk.Label(window, text="Output")
my_label.pack()
result = tk.Button(window,text="show",font=("Arial",12,"bold"),command=show).pack()
window.mainloop()
I found it! Finally :)
Thank you Derek you helped me a lot finding this solution.
the link s question answer helped me :
我用 Toplevel() 函数替换了 Tk() 并且它起作用了:)
def badper(self):
window=tk.Toplevel()
window.geometry("300x300")
window.config(bg='lightgreen')
store_var= []
def show():
total = 0
# This is one way to display results
for a in store_var:
if a.get():
total = total + 1
my_label["text"] = f"Total = {total}"
def create_check_button(i):
var = tk.IntVar()
# for i in self.badcharacter:
c = tk.Checkbutton(window, text=i,onvalue=1,offvalue=0,variable=var)
c.pack()
# Save each IntVar in list for use by function show()
store_var.append(var)
for i in self.badcharacter:
create_check_button(i)
# my_label is referenced in function show() so define my_label separately from pack
my_label = tk.Label(window, text="Output")
my_label.pack()
result = tk.Button(window,text="show",font=("Arial",12,"bold"),command=show).pack()
window.mainloop()
我正在尝试在 class 中创建一个函数 函数,我想计算点击了多少个复选框。
# This function assesses number of clicked items in my class's list
def badper(self):
window=Tk()
window.config(bg='lightgreen')
# total = 0
def show(self):
new_label = Label(window, text=var.get()).pack()
var=IntVar()
def create_check_button(self,i):
var = IntVar()
c=Checkbutton(window,text=self.badcharacter[i],variable=var)
c.pack()
# if c == 1:
# global total
# total += 1
# This loop creates checkboxes in the equivalent of number items of my list
for i in range(6):
create_check_button(self,i)
# This button should show how many items clicked so far
my_button=Button(window,text="show", font=("Arial", 12, "bold"),command=show(self)).pack()
此代码实现了正确的结果,显示的集合总数 Checkbuttons
。
代码中的注释解释了这个简化代码段中的问题。
import tkinter as tk
window = tk.Tk()
window.config(bg='lightgreen')
store_var = []
def show():
total = 0
# This is one way to display results
for a in store_var:
if a.get():
total = total + 1
my_label["text"] = f"Total = {total}"
def create_check_button(i):
var = tk.IntVar()
# no information on badcharacter list supplied
c = tk.Checkbutton(
window, text = i, onvalue = 1, offvalue = 0, variable = var)
c.pack()
# Save each IntVar in list for use by function show()
store_var.append(var)
for i in range(6):
create_check_button(i)
# my_label is referenced in function show() so define my_label separately from pack
my_label = tk.Label(window, text = "Output")
my_label.pack()
# Button is not referenced anywhere so combining declaration and pack management is OK
tk.Button( window, text = "show", font = "Arial 12 bold", command = show).pack()
window.mainloop()
这是关于我的项目的图片 I use stepI button in order to display checkbotton window
Thank you derek I am appreciated but when I clicked the show button The result shows "0"
I used the above answer in the program I saw that I could not change the result the
total
remained the same(0). So I tried different approach but this time I could not run checkbutton properly. Thestore_var
displays either "000000" or "111111" (these numbers 0,1 related with item number in my list(i))
def badper(self):
window=tk.Tk()
window.geometry("300x300")
window.config(bg='lightgreen')
store_var= []
def show():
total = 0
# This is one way to display results
for a in store_var:
if a.get():
total = total + 1
my_label["text"] = f"Total = {total}"
def create_check_button(i):
var = tk.IntVar()
# for i in self.badcharacter:
c = tk.Checkbutton(window, text=i,onvalue=1,offvalue=0,variable=var)
c.pack()
# Save each IntVar in list for use by function show()
store_var.append(var)
for i in self.badcharacter:
create_check_button(i)
# my_label is referenced in function show() so define my_label separately from pack
my_label = tk.Label(window, text="Output")
my_label.pack()
result = tk.Button(window,text="show",font=("Arial",12,"bold"),command=show).pack()
window.mainloop()
I found it! Finally :) Thank you Derek you helped me a lot finding this solution. the link s question answer helped me :
我用 Toplevel() 函数替换了 Tk() 并且它起作用了:)
def badper(self):
window=tk.Toplevel()
window.geometry("300x300")
window.config(bg='lightgreen')
store_var= []
def show():
total = 0
# This is one way to display results
for a in store_var:
if a.get():
total = total + 1
my_label["text"] = f"Total = {total}"
def create_check_button(i):
var = tk.IntVar()
# for i in self.badcharacter:
c = tk.Checkbutton(window, text=i,onvalue=1,offvalue=0,variable=var)
c.pack()
# Save each IntVar in list for use by function show()
store_var.append(var)
for i in self.badcharacter:
create_check_button(i)
# my_label is referenced in function show() so define my_label separately from pack
my_label = tk.Label(window, text="Output")
my_label.pack()
result = tk.Button(window,text="show",font=("Arial",12,"bold"),command=show).pack()
window.mainloop()