简体   繁体   English

MessageBox 未显示正确的 output

[英]MessageBox not displaying the correct output

I'm trying to use messagebox to display the output of my function however while the function displays in the terminal it doesn't display in the messagebox.我正在尝试使用消息框来显示我的 function 的 output 但是,虽然 function 显示在终端中,但它没有显示在消息框中。 Is messagebox the correct one or is there another function I should be using?消息框是正确的还是我应该使用的另一个 function? I simply want the x words to display on the GUI.我只是想让 x 单词显示在 GUI 上。

#Imports
import tkinter as tk
from tkinter import *
from tkinter import filedialog
from collections import Counter
from tkinter import messagebox
import collections

# Initialize the dictionary
wordcount = {}

#open Macbeth text file
file = open('Macbeth Entire Play.txt', encoding="utf8")
a= file.read()

class Application(tk.Frame):
    def __init__(self, master):
        super().__init__()  # Call __init__() method in parent (tk.Frame)
        
        self.label = tk.Button(self, text='How many words to Sort?', command=self.ask_count)
        self.label.grid(row=0)
        self.open_btn = tk.Button(text='Compute', command=self.ask_count)
        self.open_btn.pack(pady=(30,10))
        self.exit_btn = tk.Button(text='Exit', command=master.destroy)
        self.exit_btn.pack()

    def ask_count(self):
        
        with open('Macbeth Entire Play.txt', encoding="utf8") as file:
            self.file_text = file.read()
        for word in a.lower().split():
          word = word.replace(".","")
          word = word.replace(",","")
          word = word.replace(":","")
          word = word.replace("\"","")
          word = word.replace("!","")
          word = word.replace("“","")
          word = word.replace("‘","")
          word = word.replace("*","")
          if word not in wordcount:
              wordcount[word] = 1
          else:
              wordcount[word] += 1
        n_print = int(input("How many most common words are: "))
        print("\nThe {} most common words are as follows\n".format(n_print))
        word_counter = collections.Counter(wordcount)
        for word, count in word_counter.most_common(n_print):
          print(word, ": ", count)
        messagebox.showinfo("The top words are: " == n_print)

        # Close the file
        file.close()
        messagebox.showinfo("The top words are: ")

if __name__ == '__main__':
    root = tk.Tk()
    root.title("Count words")
    root.geometry('400x400+900+50')
    app = Application(root)
    app.pack(expand=True, fill='both')
    root.mainloop()

It seems that the line self.label(text='How many words to Sort?', command=self.ask_count).grid(row=0) is causing the error.似乎self.label(text='How many words to Sort?', command=self.ask_count).grid(row=0)导致了错误。

In this line, it seems that you are trying to call the label() function from self , but there is no label method in class Application(tk.Frame) .在这一行中,您似乎正在尝试从self调用label() function ,但是class Application(tk.Frame)中没有label方法。 Also, Tkinter labels don't have a command argument, so you'll have to use a button instead.此外,Tkinter 标签没有command参数,因此您必须使用按钮。 It is also a good idea to store the button in a variable and call grid() on a separate line.将按钮存储在变量中并在单独的行上调用grid()也是一个好主意。 This allows you to keep a reference to the button object.这允许您保留对按钮 object 的引用。

Try replacing that line with this:尝试用以下代码替换该行:

self.label = tk.Button(self, text='How many words to Sort?', command=self.ask_count)
self.label.grid(row=0)

This will create a button and put it inside the application, which is what it seems you are trying to do.这将创建一个按钮并将其放入应用程序中,这似乎是您正在尝试做的事情。

EDIT: It looks like the original question has been modified.编辑:看起来原始问题已被修改。 To answer the new question, replace the line messagebox.showinfo("The top words are: " == n_print) with the following:要回答新问题,请将messagebox.showinfo("The top words are: " == n_print)行替换为以下内容:

 top_words = [(str(word)+": "+str(count)) for word, count in word_counter.most_common(n_print)] messagebox.showinfo("Top words...", "The top words are: \n" + "\n".join(top_words))

You wrote this:你写了这个:

class Application(tk.Frame):
    def __init__(self, master):
        super().__init__()  # Call __init__() method in parent (tk.Frame)
        
        self.label(text='How many words to Sort?', command=self.ask_count).grid(row=0)

which doesn't make sense as self.label isn't defined.这没有意义,因为self.label没有定义。 My guess is that you wanted to write我的猜测是你想写

class Application(tk.Frame):
    def __init__(self, master):
        super().__init__()  # Call __init__() method in parent (tk.Frame)
        
        self.label = tk.Button(text='How many words to Sort?', command=self.ask_count)
        self.label.grid(row=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM