python-3.x - Tkinter 显示类列表文本
问题描述
我有以下代码:我正在尝试从函数 showText 获取文本以实际出现在窗口中
from tkinter import *
import wikipedia
from nltk.tokenize import sent_tokenize, word_tokenize
import time
subject = wikipedia.page("Assembly language")
#print(p.url)
#print(p.title)
plain_text = subject.content
plain_text_words = word_tokenize(plain_text)
class Window(Frame):
def __init__(self, master= None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title('Test')
self.pack(fill=BOTH, expand =1)
quitButton = Button(self, text = 'Exit', command=self.client_exit)
quitButton.place(x=500, y=300)
menu = Menu(self.master)
self.master.config(menu=menu)
file = Menu(menu)
menu.add_cascade(label='File', menu=file)
file.add_command(label='Run', command=self.showText)
file.add_command(label='Exit', command=self.client_exit)
help_menu = Menu(menu)
menu.add_cascade(label='Help', menu=help_menu)
help_menu.add_command(label='About')
def showText(self):
for i in range (0, len(plain_text_words)):
words = [i]
time.sleep(3)
words.pack()
def client_exit(self):
exit()
root = Tk()
w = 600 # width of the window
h = 370 # height of the window
# get screen width and height
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
# calculate x and y coordinates for the Tk root window
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
让我伤心的部分是这样的:
def showText(self):
for i in range (0, len(plain_text_words)):
words = [i]
time.sleep(3)
words.pack()
我试图让每段文本在窗口上显示为单个单词,但无论我如何尝试,我都会收到错误消息。我已经尝试过各种事情,例如转换为列表等希望有人可以提供帮助..
PS:在运行此代码之前,您需要下载 nltk 数据集
解决方案
首先,您的 pack 函数缺少括号。其次,您的 varwords
是一个列表。不是 tkinter 小部件。您只能打包 tkinter 小部件。
例如,要解决它,您需要将列表添加到文本小部件。
推荐阅读
- mysql - 如何从表格列中选择 4 位数字的组合?
- c++ - 为什么T2抛出bad_alloc异常时这段代码会发生内存泄漏
- google-cloud-source-repos - 如何在 AWS 实例中克隆 GCP 云源存储库
- swift - 访问结构内的结构以返回排序数组
- c++ - C++ 在派生类中排除通过基类继承的类
- mysql - 为什么`if(id = max(id),id,id + 1)`在mysql中不能按预期工作
- scala - Scala:为什么 Int 类型列表也包含 String?
- android - 再次登录会删除 Firebase 实时数据库中的数据库
- r - 如何减少 R 列表中的值
- java - RegEx 不会从字符串中删除字符