Python/tkinter - defining checkbox looping over a string of labels - python-3.x

I want to define a row of tk checkbox widget looping on a list of labels. Everything works fine except that I can't seem to be able to set the IntVar value on the if statement, although it works for the state. What am I doing wrong?
def CheckboxLine(self, frame, fformat=None):
self.__set_col(0) # set widget column to zero
var_c = []
widget_c = []
if fformat is None:
fformat = ['text', 'excel', 'xml']
# widget definition
for item in fformat:
var_c.append(tk.IntVar(master=frame))
widget_c.append(tk.Checkbutton(master=frame, text=item, variable=var_c[-1]))
if item == 'text':
var_c[-1].set(1)
widget_c[-1]['state']='disabled'
else:
var_c[-1].set(0)
widget_c[-1]['state']='normal'
widget_c[-1].grid(row=self.__row, column=self.__col, columnspan=1, padx=self.__padx, pady=self.__pady, sticky="nsew")
self.__next_col()
self.__next_row()

Define var_c and widget_c outside of your function. The reason behind this is unclear to me, unfortunately.
import tkinter as tk
root = tk.Tk()
var_c = []
widget_c = []
def CheckboxLine(frame, fformat=None):
if fformat is None:
fformat = ['text', 'excel', 'xml']
# widget definition
for idx, item in enumerate(fformat):
var_c.append(tk.IntVar(value=0))
widget_c.append(tk.Checkbutton(master=frame, text=item, variable=var_c[-1]))
if item == "text":
var_c[-1].set(1)
widget_c[-1]["state"] = "disabled"
widget_c[-1].grid(row=idx, column=0)
CheckboxLine(root)
root.mainloop()

Related

How to get the values of all the OptionMenu widgets within a frame inside a canvas in Tkinter

I'm writing a minimalist image tagging app that will list out all the image files in a specific location alongside a dropdown menu to select the options for tagging the image. Once the images are tagged, I need to save the changes to a JSON file and I've got a button for that. How can we read all the options selected so that it can be written into a file?
Following is the code so far:
from tkinter import N, RIGHT, Button, OptionMenu, Scrollbar, StringVar, Tk, Canvas, Frame, Label
class App:
def __init__(self):
self.root = Tk()
self.tags = ['Apples', 'Oranges', 'Berries']
self.GetRows()
self.SaveButton()
self.root.mainloop()
def GetRows(self):
self.canvas = Canvas(self.root)
self.scroll_y = Scrollbar(self.root, orient="vertical", command=self.canvas.yview)
self.frame = Frame(self.canvas)
lst = [f"A01{str(i)}.JPG" for i in range(100)]
for idx, r in enumerate(lst):
filename = Label(self.frame, text=r)
filename.grid(row=idx+2, column=0, sticky=N)
label = StringVar()
drop = OptionMenu(self.frame, label, *self.tags)
drop.grid(row=idx+2, column=1)
# put the frame in the canvas
self.canvas.create_window(0, 0, anchor='nw', window=self.frame)
# make sure everything is displayed before configuring the scrollregion
self.canvas.update_idletasks()
self.canvas.configure(scrollregion=self.canvas.bbox('all'),
yscrollcommand=self.scroll_y.set)
self.canvas.pack(fill='both', expand=True, side='left')
self.scroll_y.pack(fill='y', side='right')
def SaveState(self):
pass
def SaveButton(self):
self.save_button = Button(self.root, text="Save Changes", padx=50, pady=10, command=self.SaveState)
self.save_button.pack(side=RIGHT)
if __name__ == '__main__':
App()
The SaveState method is what will be used to write the selections so far into a file.
Thanks in advance!
In order to make OptionMenu results available try modifying your code so that
all StringVars are accessible outside of GetRows.
def GetRows(self):
...
# Define label as a list
self.label = []
for idx, r in enumerate(lst):
filename = Label(self.frame, text=r)
filename.grid(row=idx+2, column=0, sticky=N)
label = StringVar()
drop = OptionMenu(self.frame, label, *self.tags)
drop.grid(row=idx+2, column=1)
# Save StringVar reference
self.label.append(label)
...
def SaveState(self):
self.data = dict()
# Retrieve results into dictionary
for i, a in enumerate(self.label):
self.data[f"A_{i}"] = a.get()
print(self.data)
Then use json.dump(self.data, a_file) to save it

Creating a Search Bar to filter & Update, however, idle seems to be loading / in a hidden loop with no returning value

Without clicking the search button everything works fine and returns a value.
However, using the search button will cause the code to pause.
The main issue is the Check() nested in chooseBoard() which calls in on itself causing a constant 'hidden loop'.
This means that the code doesn't carry on from:
board = tempPreset.chooseBoard(None)
and unable to return wanted value.
In short, my question is how do I fix the nested loop issue of not carrying on after the check() is completed when the search button is pressed. As the value is correct when I print it from there.
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
class PresetUpdate:
def __init__(self,master,area,board):
self.root = master
self.root.title('Preset')
self.root.geometry(area)
self.board = board
def boardId(self,name):
self.name = name
self.var.set(name)
self.root.destroy()
def chooseBoard(self, newBoard):
def check():
#set variables
self.newBoard = {}
r = re.compile(self.e.get(), re.IGNORECASE)
#regex to see words in list
for k in self.board:
if r.search(k) != None:
self.newBoard.update({r.search(k).string : self.board[r.search(k).string]})
if self.newBoard == {}:
self.newBoard = self.board
self.root.destroy()
#creating new root
master = tk.Tk()
self.__init__(master,'460x475',self.board)
#re-calling function with update list
self.chooseBoard(self.newBoard)
return self.board[self.name]
#print(self.board[self.name])
#set variables
self.newBoard = self.board if newBoard is None else newBoard
self.var = tk.StringVar()
self.button = {}
#setting search bar / button
self.e = tk.Entry(self.root,width=30)
self.e.grid(row=0,column=2,padx=10,pady=10)
click = tk.Button(self.root, text='Search', height=1,width=5,command=check)
click.grid(row=0,column=1,padx=10,pady=10)
#creating buttons of dct
for i,boardName in enumerate(self.newBoard):
#print(i,boardName,self.board[boardName])
self.button[i] = tk.Button(self.root, text=boardName,height=2,width=30,command=lambda name=boardName:self.boardId(name))
self.button[i].grid(row=i+1,column=1,padx=10,pady=10)
#pause until a button has been pressed
self.button[i].wait_variable(self.var)
return self.board[self.name]
r = {'test1' : '0','test2' : '1','test3' : '2','test4' : '3','test5' : '4'}
def main():
if __name__ == '__main__':
master = tk.Tk()
tempPreset = PresetUpdate(master,'460x475',r)
board = tempPreset.chooseBoard(None)
print(board)
master.mainloop()
main()
I found the solution:
def chooseBoard(self, board):
def check():
#set variables
self.newBoard = {}
r = re.compile(self.e.get(), re.IGNORECASE)
#regex to see words in list
for k in self.board:
if r.search(k) != None:
self.newBoard.update({r.search(k).string : self.board[r.search(k).string]})
if self.newBoard == {}:
self.newBoard = self.board
for i,item in enumerate(self.board):
if item not in self.newBoard:
self.button[i].destroy()
if self.newBoard == self.board:
for i,item in enumerate(self.board):
self.button[i].destroy()
createButtons()
#set variables
self.newBoard = self.board if self.newBoard is None else self.newBoard
self.var = tk.StringVar()
self.button = {}
#setting search bar / button
self.e = tk.Entry(self.root,width=30)
self.e.grid(row=0,column=2,padx=10,pady=10)
click = tk.Button(self.root, text='Search', height=1,width=5,command=check)
click.grid(row=0,column=1,padx=10,pady=10)
#creating buttons of dct
def createButtons():
for i,boardName in enumerate(self.newBoard):
#print(i,boardName,self.board[boardName])
self.button[i] = tk.Button(self.root, text=boardName,height=2,width=30,command=lambda name=boardName:self.boardId(name))
self.button[i].grid(row=i+1,column=1,padx=10,pady=10)
return i
i = createButtons()
#check()
#pause until a button has been pressed
self.button[i].wait_variable(self.var)
return self.board[self.name]

Python: Sorting based on class attribute

I am a rookie making a to-do list-GUI in Tkinter. Every task in the to-do list has a class attribute associated with it.
So a task is an object of Task: and it has a value attribute self.value.
I want to sort all the tasks displayed in the GUI based on the attribute self.value. So for instance the task with the highest value is displayed on top of the list and the task with the lowest value is displayed at the bottom of the list.
Note that it is not the value that must be displayed in the gui but the attribute self.name but it has to be displayed in an order based on the self.value.
I only understand conceptually what to do (i think). Somethng like, i need to make a list based on the self.value, then sort the "items" in the list from highest to lowest value, and then display the task.namein the gui based on the ordering of self.value.
Maybe I am retarded?
What would be the norm to do here?
EDIT
Code:
from tkinter import Tk, Frame, Button, Entry, Label, Toplevel
import tkinter.messagebox # Import the messagebox module
task_list = []
#value_list = []
class Task:
def __init__(self, n, h):
self.name = n
self.hours = h
def show_tasks():
task = task_list[-1]
print('\n')
print('_______________________')
print('\n')
print('Task:')
print(task.name)
print('\n')
print('Hours')
print(task.hours)
def open_add_task():
taskwin = Toplevel(root)
taskwin.focus_force()
#NAME
titlelabel = Label(taskwin, text='Title task concisely:', font=('Roboto',11,'bold')).grid(column=1, row=0)
name_entry = Entry(taskwin, width=40, justify='center')
name_entry.grid(column=1, row=1)
#HOURS
hourlabel = Label(taskwin, text='Whole hours \n required', font=('Roboto',10)).grid(column=1, row=2)
hour_entry = Entry(taskwin, width=4, justify='center')
hour_entry.grid(column=1, row=3)
def add_task():
if name_entry.get() != '':
task_list.append(Task(name_entry.get(), hour_entry.get()))
show_tasks()
listbox_tasks.insert(tkinter.END, name_entry.get())
name_entry.delete(0, tkinter.END)
taskwin.destroy()
else:
tkinter.messagebox.showwarning(title='Whoops', message='You must enter a task')
Add_button = Button(taskwin, text='Add', font=('Roboto',10), command=add_task).grid(column=1, row=4)
def sort_tasks():
pass
root = Tk()
task_frame = Frame()
# Create UI
your_tasks_label = Label(root, text='THESE ARE YOUR TASKS:', font=('Roboto',10, 'bold'), justify='center')
your_tasks_label.pack()
listbox_tasks = tkinter.Listbox(root, height=10, width=50, font=('Roboto',10), justify='center') # tkinter.Listbox(where it should go, height=x, width=xx)
listbox_tasks.pack()
#BUTTONS
New_Task_Button = Button(root, text='New Task', width=42, command=open_add_task)
New_Task_Button.pack()
root.mainloop()
There are multiple ways to do what you are trying to achieve.
The simplest way of all is to use the list.sort method and set reverse=True, which will sort in descending order. Assuming all your list values will be Task instances, In your case, you could do
todo_list.sort(key=lambda a: a.value, reverse=True)
Assuming your value attributes are integers. I made a small example.
names_values={"name1": 5, "name2": 10, "name3": 2}
todo_list = []
class Task:
def __init__(self, name: str, value: int):
self.name = name
self.value = value
for key, value in names_values.items():
task = Task(key, value)
todo_list.append(task)
print("Before sorting: ", todo_list)
print("\nBefore sorting values: ", [(x.name, x.value) for x in todo_list])
todo_list.sort(key=lambda a: a.value, reverse=True)
print("\nAfter sorting: ", todo_list)
print("\nAfter sorting values: ", [(x.name, x.value) for x in todo_list])
Now you have a sorted list. You can now, iterate through list and use object.name to insert it to your list view.
updating since you added MRE:
Since you also need to show the sorted list in the GUI. you need to reload your list view. One way would to clear your list view and insert it again is as shown below.
...
def add_task():
if name_entry.get() != '':
task_list.append(Task(name_entry.get(), int(hour_entry.get())))
show_tasks()
reload()
taskwin.destroy()
else:
tkinter.messagebox.showwarning(title='Whoops', message='You must enter a task')
Add_button = Button(taskwin, text='Add', font=('Roboto',10), command=add_task).grid(column=1, row=4)
def reload():
task_list.sort(key=lambda a: a.hours, reverse=True)
listbox_tasks.delete(0, tkinter.END)
for x in task_list:
listbox_tasks.insert(tkinter.END, x.name)
...

Automatically switching checkboxes on tkinter

I would like to switch on the checkboxes (0, 2, 4) automatically with the click of a button. I have the following code. For some reason it dont work. Please help me.
from tkinter import *
class Error(Frame):
def Widgets(self):
for i in range(len(self.X)):
self.X[i] = Checkbutton(self, text="%d"%(i,))
self.X[i].grid(row=i, sticky=W)
self.X[i].configure(variable = ("var_%d"%(i,)))
self.button = Button(self, text = "set", command = self.test)
self.button.grid(row=5, sticky=W)
def test(self):
for i in range(len(self.X)):
if i == 0 or i == 2 or i == 4:
set (("var_%d"%(i,))) == 1
def __init__(self,initial):
super(Error,self).__init__(initial)
self.X = [{},{},{},{},{}]
self.grid()
self.Widgets()
Window = Tk()
Tool = Error(Window)
Window.mainloop()
The way to handle checkboxes is to associate each box with a variable which reflects wether the box is checked or not.
For an array of checkboxes it is convenient to store these variables in a list. The way I would do it is to create an empty list and then append variables as I go along.
In the function test() I use enumerate in the for-loop as this is the recommended way to generate an index of the list.
from tkinter import *
class Error(Frame):
def __init__(self, master):
super(Error,self).__init__(master)
self.box_list = [] # List to holld checbox variables
self.grid()
self.Widgets()
def Widgets(self):
for i in range(5):
var = BooleanVar() # Create variable to associate with box
cb = Checkbutton(self, text="%d"%(i,))
cb.grid(row=i, sticky=W)
cb.configure(variable=var)
self.box_list.append(var) # Append checkbox variable to list
self.button = Button(self, text = "set", command = self.test)
self.button.grid(row=5, sticky=W)
def test(self):
for i, var in enumerate(self.box_list):
if i == 0 or i == 2 or i == 4:
var.set(True)
Window = Tk()
Tool = Error(Window)
Window.mainloop()

iterate through a list and get user response to each item using tkinter GUI

I am being particularly obtuse. I am iterating through a list of technical italian words and wanting to insert a translation using a tkinter interface. There is no problem doing this without the GUI: My problem is that I cannot figure out how to do an iteration, load a word into a ttk.Label and wait for a user entry in a ttk.Entry field. I have searched and found explanations, but I am at a loss how to apply the suggestions. This is my code using a trivial list of words:
from tkinter import ttk
import tkinter as tk
def formd():
list_of_terms = ['aardvark', 'ant','zombie', 'cat', 'dog', 'buffalo','eagle', 'owl','caterpiller', 'zebra', 'orchid','arum lily' ]
discard_list = []
temp_dict={}
list_of_terms.sort()
for item in list_of_terms:
listKey.set(item)
# need to wait and get user input
translation =dictValue.get()
temp_dict[item]=translation
discard_list.append(item)
# check if it has worked
for key, value in temp_dict.items():
print(key, value)
# GUI for dict from list
LARGE_FONT= ("Comic sans MS", 12)
root = tk.Tk()
root.title('Nautical Term Bilingual Dictionary')
ttk.Style().configure("mybtn.TButton", font = ('Comic sans MS','12'), padding = 1, foreground = 'DodgerBlue4')
ttk.Style().configure('red.TButton', foreground='red', padding=6, font=('Comic sans MS',' 10'))
ttk.Style().configure('action.TLabelframe', foreground = 'dodger blue3')
#.......contents frames.....................
nb = ttk.Notebook(root)
page5 = ttk.Frame(nb)
# declare variables
listKey= tk.StringVar()
dictValue = tk.StringVar()
# widgets
keyLabel =ttk.Label( page5, text = "key from list", font=LARGE_FONT).grid(row=3, column = 0)
Keyfromlist =ttk.Label(page5, width = 10, textvariable = listKey).grid(row = 3, column = 1)
valueLabel =ttk.Label( page5, text = "enter translation", font=LARGE_FONT).grid(row=3, column = 2)
listValue =ttk.Entry(page5, textvariable =dictValue, width = 15).grid(row = 3, column = 3)
#listValue.delete(0,'end')
#listValue.focus_set()
# add buttons
b1 = ttk.Button(page5, text="add to dictionary",style = "mybtn.TButton", command = formd)
b1.grid(row = 5, column = 0)
b5 = ttk.Button(page5, text="clear entry", style ="mybtn.TButton")
b5.grid(row = 5, column = 2)
nb.add(page5, text='From List')
nb.pack(expand=1, fill="both")
for child in root.winfo_children():
child.grid_configure(padx =5, pady=5)
if __name__ == "__main__":
root.mainloop()
I wonder whether someone could take the time to suggest a solution, please. How to stop a while loop to get input from user using Tkinter? was the one suggestion that I cannot figure how to use in my example
tkinter doesn't "play nice" with while loops.
Fortunately for you, you don't need to use one.
You can do something like the below:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.list = ['aardvark', 'ant','zombie', 'cat', 'dog', 'buffalo','eagle', 'owl','caterpiller', 'zebra', 'orchid','arum lily' ]
self.text = Label(self.root, text="aardvark")
self.entry = Entry(self.root)
self.button = Button(self.root, text="Ok", command=self.command)
self.text.pack()
self.entry.pack()
self.button.pack()
def command(self):
print(self.text.cget("text")+" - "+self.entry.get())
try:
self.text.configure(text = self.list[self.list.index(self.text.cget("text"))+1])
except IndexError:
self.entry.destroy()
self.button.destroy()
self.text.configure(text = "You have completed the test")
root = Tk()
App(root)
root.mainloop()
This essentially uses the Button widget to iterate to the next text and get the next input.

Resources