I have the following code that creates 4 rows of 7 checkboxes using Flet:
import flet as ft
def main(page):
DaysOfWeek = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
checkboxes=[]
for j in range(0,4):
def boxes(count):
boxes = []
for i in range(0,7):
boxes.append(ft.Checkbox(label=DaysOfWeek[i]))
return boxes
row2 = ft.Row(spacing=30, controls=boxes(7))
page.add(row2)
ft.app(target=main)
What I need to do now is determine which of the checkboxes in each row have been checked by the user. Can anyone point me in the right direction? Thanks in advance for any constructive suggestions.
I was able to figure out an answer to this myself. The code below creates 4 rows of 7 checkboxes (one for every day of the week). It then lets the user check the boxes they want and when they click on the "Print Checked Boxes" button the list of lists is printed to the console. The indices of the checkboxes for each row are placed into a list. Once all four rows have been processed, each list is added to a final list. I added a current time display since I'll also need that for my ultimate project. I'm new to Python and Flet so I'm sure there are more efficient ways of getting this done. I'm open to suggestions.
import flet as ft
import time
def main(page: ft.page):
# Get the current time and add it to the page in a container
now = time.strftime("%I:%M:%S")
my_time = ft.Text(now, text_align="center", size=30, weight="bold")
page.add(my_time)
DaysOfWeek = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
CheckOnArray=[]
CheckOn=[]
# Create a dictionary to hold the lists
valves = {}
# Create the lists and add them to the dictionary
for i in range(1, 5):
valve_name = f"valve{i}"
valves[valve_name] = []
#print(valve_name)
for j in range(0,4):
global array
array = []
for i in range(0,7):
#print(j,i)
CheckOn = ft.Checkbox(label=DaysOfWeek[i], value=False)
#print(CheckOn.value)
CheckOnArray.append(CheckOn.value)
#print(CheckOnArray)
array.append(ft.Checkbox(label=DaysOfWeek[i], value=False))
#print(array)
valve_name = f"valve{j}"
valves[valve_name] = array
#print(valve_name)
row = ft.Row(spacing=30, controls=array)
page.add(row)
def button_clicked(e):
OnDays1=[]
OnDays2=[]
OnDays3=[]
OnDays4=[]
# Example code to print the index and value of each checkbox
for i, checkbox in enumerate(valves["valve0"]):
if checkbox.value == True:
OnDays1.append(i)
print(OnDays1)
for i, checkbox in enumerate(valves["valve1"]):
if checkbox.value == True:
OnDays2.append(i)
print(OnDays2)
for i, checkbox in enumerate(valves["valve2"]):
if checkbox.value == True:
OnDays3.append(i)
print(OnDays3)
for i, checkbox in enumerate(valves["valve3"]):
if checkbox.value == True:
OnDays4.append(i)
print(OnDays4)
# Join the lists into a single string
OnDaysAll = f"[{str(OnDays1)}, {str(OnDays2)}, {str(OnDays3)}, {str(OnDays4)}]"
print(OnDaysAll)
b = ft.ElevatedButton("Print checked boxes", on_click=button_clicked)
page.add(b)
#Display current time every second
def displayTime():
while True:
now = time.strftime("%I:%M:%S")
my_time.value = now
page.update()
time.sleep(1)
page.update()
displayTime()
page.update()
ft.app(target=main)