douglas rouse
douglas rouse

Reputation: 146

Update Tkinter Label text from text file

I have a file usercheck.txt with a list of names on each line so for example my file would look like this.

Kevin
Bob
Sally
Ronnie
O'sullivan

If someone launches the Tkinter program, their name is automatically added to the list. When they close it, their name is automatically removed from the text file. I want to implement a label which updates in real time to see who is online. When i try and update my label with the following code (which is not quite my code but similar enough so my problem is easily reproduced) I get this error.

Traceback (most recent call last):
    File "C:\Users\Douglas Rouse\Google Drive\Python\throwaway.py", line 16, in <module>
      upd()
    File "C:\Users\Douglas Rouse\Google Drive\Python\throwaway.py", line 13, in upd
      attempt.config(d)
    File "C:\Users\Douglas Rouse\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1330, in configure
      return self._configure('configure', cnf, kw)
    File "C:\Users\Douglas Rouse\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1316, in _configure
      cnf = _cnfmerge(cnf)
    File "C:\Users\Douglas Rouse\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 100, in _cnfmerge
      for c in _flatten(cnfs):
TypeError: object of type 'builtin_function_or_method' has no len()

Here is my code:

import tkinter as tk

root = tk.Tk()

f = open("usercheck.txt", "r").readlines()
attempt = tk.Label(root, text="\n".join(f),bg = "#42f480")
attempt.grid(row=0,column =5)

def upd():
    d = open ("usercheck.txt","r").read
    attempt.config(d)

upd()

Edit I realize that I forgot the parentheses on .read()

the new error I get is:

Traceback (most recent call last):
  File "C:\Users\Douglas Rouse\Google Drive\Python\throwaway.py", line 13, in <module>
        upd()
  File "C:\Users\Douglas Rouse\Google Drive\Python\throwaway.py", line 11, in upd
    attempt.config(d)
  File "C:\Users\Douglas Rouse\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1330, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Users\Douglas Rouse\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1320, in _configure
    return self._getconfigure1(_flatten((self._w, cmd, '-'+cnf)))
  File "C:\Users\Douglas Rouse\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1308, in _getconfigure1
    x = self.tk.splitlist(self.tk.call(*args))
tkinter.TclError: unknown option 
"-Kevin
Bob
Sally
Ronnie
O'sullivan"

Upvotes: 1

Views: 1378

Answers (1)

Tom Fuller
Tom Fuller

Reputation: 5349

The function config can be used for lots of different widgets in tkinter, it is used to change something about that widget, like width, height, text or font, i'm assuming you want to change the text on your label

Change attempt.config(d)

To attempt.config(text = d)

Upvotes: 1

Related Questions