从文本文件更新Tkinter标签文本
问题描述:
我有一个文件usercheck.txt
与每行的名称列表,例如我的文件将如下所示。从文本文件更新Tkinter标签文本
Kevin
Bob
Sally
Ronnie
O'sullivan
如果有人启动Tkinter程序,他们的名字会自动添加到列表中。当他们关闭它时,他们的名字会自动从文本文件中删除。我想实现一个实时更新的标签,以查看谁在线。当我尝试更新我的标签与下面的代码(这不是我的代码,但足够相似,所以我的问题很容易再现),我得到这个错误。
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()
这里是我的代码:
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()
编辑 我意识到,我忘了括号上.read()
新的错误我得到的是:
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"
答
功能config
可用于tkinter中的很多不同的小部件,它用于改变有关该小部件的内容,例如宽度,高度,文本或字体,我假设您要更改标签上的文本
变化attempt.config(d)
要attempt.config(text = d)
这一段需要一些标点符号:“我想实现一个标志,实时更新,看看谁在线然而,当我试着用下面的代码是更新我的标签不是我的代码,但足够相似,所以我的问题很容易转载我得到这个错误。“ – nbro