Home > other >  Treeview: How to set values in a specific row where row contains "x" value?
Treeview: How to set values in a specific row where row contains "x" value?

Time:11-24

I have a treeview with the following columns:

self.columns = ("Name", "Status", "Activity")

This treeview is updated depending on the socket message and client name it receives. If the program receives "NAME:", it will insert a new row in the treeview with the client name placed under the "Name" column. Else if it's "CLOSED:", the "Status" and "Activity" columns where client name given is located will be updated.

import tkinter as tk
from tkinter import ttk as tick
import socket
from threading import Thread

class GUI2(cust.CTk): #second window, not the root
    def __init__(self, a, b, c, d, e):

        self.PORT = a
        self.SERVER = b
        self.ADDRESS = c
        self.FORMAT = d
        self.host = e

        self.master2 = cust.CTkToplevel()
        self.columns = ("name", "status", "activity")

        self.clientlist = tick.Treeview(self.clientframe, columns = self.columns, show = "tree")
        self.clientlist.grid(row = 0, column = 0, sticky = "nswe")

        self.clientlist.column("#0", minwidth = 0, width = 10, stretch = False)
        self.clientlist.column("name", minwidth = 0, width = 140, stretch = False)
        self.clientlist.column("status", minwidth = 0, width = 140, stretch = False)
        self.clientlist.column("activity", minwidth = 0, width = 140, stretch = False)
        
        self.thread = Thread(target = self.initreceiver)
        self.thread.start()
        
    def initreceiver(self):

        try:
            while True:

                self.message = self.host.recv(1024).decode(self.FORMAT)

                if "NAME:" in self.message:
                    x = self.message.replace("NAME:", "") #removes "NAME:" to get the clientname

                    self.clientlist.insert("", cust.END, iid = x, values = x) #inserts new row and display only client name; 
            #also sets iid the same as the client name for reference

                elif "CLOSED:" in self.message:
                    x = self.message.replace("CLOSED", "") #remove "CLOSED:" to get clientname

                    self.clientlist.set(x, "Status", "Eyes are closed") #set "Status" column with the new value
                    self.clientlist.set(x, "Activity", "Eyes are closed") #set "Activity" column with the new value

        except Exception:

            print (traceback.format_exc())

Error is as follows:

Traceback (most recent call last):
  File "F:\Personal Programs\Python\Lobby   Tracking\mainmenu.py", line 438, in initreceiver
    self.clientlist.set(x, "status", "Eyes are closed")
  File "F:\Program Files (x86)\Python\lib\tkinter\ttk.py", line 1459, in set
    res = self.tk.call(self._w, "set", item, column, value)
_tkinter.TclError: Item :Maikz not found #Maikz is the example client name sent

It looks like .set() method needs item value for the first argument, but I don't know what to put; I'm learning Treeview for the first time.

I need to be able to set the "Status" and "Activity" cells' values where their "Name" value matches the client name. Any advice or alternative solution is appreciated.

CodePudding user response:

As suggested in my comment, you can split the received message into action and name and use action in if checking and name as the row ID:

def initreceiver(self):
    try:
        while True:
            self.message = self.host.recv(1024).decode(self.FORMAT)
            action, name = self.message.split(":")
            if action == "NAME":
                self.clientlist.insert("", cust.END, iid=name, values=name)
            elif action == "CLOSED":
                self.clientlist.set(name, "status", "Eyes are closed")
                self.clientlist.set(name, "activity", "Eyes are closed")
    except:
        print(traceback.format_exc())

CodePudding user response:

Thanks to @acw1668, it turns out the cause of the error was a spelling mistake; in x = self.message.replace("CLOSED", "") a : was missing.

To set the values of "Status" and "Activity" where the "Name" value is the same as the client name, the code is as follows:

elif "CLOSED:" in self.message:
    x = self.message.replace("CLOSED:", "") 
    self.clientlist.set(x, "Status", "Eyes are closed") 
    self.clientlist.set(x, "Activity", "Eyes are closed")
    

where x is the client name and is also the item iid as previously set in the line self.clientlist.insert("", cust.END, iid = x, values = x) when inserting a new row.

  • Related