2

The text source consists out of elements from a nested lists. A label is created for each element in the list. Below the code:

list1 = [["test", 3, 2, 0], ["test2", 4, 1, 1],["test3", 0, 5, 2]]
row = 1
for i in list1:
    text = "name: {}, x: {}, y: {}, z: {}".format(i[0],i[1],i[2],i[3])
    customtkinter.CTkLabel(master=self.frame_1, text=text).grid(row=row, column=0, sticky='nw')
    row = row + 1

Now i want i[3] to have a color. The question is, how?

Zercon
  • 105
  • 9

1 Answers1

3

The easiest way to do this is to create multiple labels in a frame and set the colour of the one you want.

list1 = [["test", 3, 2, 0], ["test2", 4, 1, 1],["test3", 0, 5, 2]]
row = 1
for i in list1:
    label_wrapper = customtkinter.CTkFrame(master = self.frame_1)
    label_wrapper.grid(row=row, column=0, sticky='nw')
    keys = ["name", "x", "y", "z"]
    for n, v in enumerate(i):
        text = "{}: {}".format(keys[n], v)
        label = customtkinter.CTkLabel(master=label_wrapper, text=text)
        if n == 3:
            label.configure(fg = "red")
        label.grid(row = 0, column = n)
    row = row + 1
Henry
  • 3,472
  • 2
  • 12
  • 36
  • now it makes the whole part of: z: 0 red, instead of just making the 0 red. I only want the variable to be a color. Is this possible? – Zercon Jul 18 '22 at 16:34
  • Nvm, i made the keys redundent by just making the first row equal to the keys so that each column just has its own name. thank you for your answer, it helped a lot! – Zercon Jul 18 '22 at 17:28
  • https://stackoverflow.com/questions/73027224/python-tkinter-destroy-not-working-correctly-with-pops-in-loop is the loop used in your answer, it has problems with pop(), i have described the problem in detail there. If you got time to look at it, its much appreciated. – Zercon Jul 18 '22 at 21:00
  • Sorry, I didn't see this earlier. Glad I could help though :) – Henry Jul 19 '22 at 21:40