Tkinter显示类别列表文本

问题描述 投票:0回答:1

我有以下代码:我试图从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数据集

python-3.x tkinter tokenize
1个回答
0
投票

首先,您的pack功能缺少括号。其次,您的var words是一个列表。不是tkinter小部件。您只能打包tkinter小部件。

例如,您需要将列表添加到文本小部件中。

© www.soinside.com 2019 - 2024. All rights reserved.