0

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. Is messagebox the correct one or is there another function I should be using? I simply want the x words to display on the 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()

5
  • Your first message box will print the result of the comparison "The top words are: " == n_print, which I imagine will be false. The second will just print "The top words are: ". I assume you intended to write the messages differently than you actually did. Commented Mar 3, 2021 at 13:50
  • @Kemp I want the messagebox to display the output of my function inside of the GUI instead of just in the terminal Commented Mar 3, 2021 at 14:56
  • you're using the correct functions from tkinter.messagebox. However looks like you may wanna rethink the logic there as kemp tried to assess. Commented Mar 3, 2021 at 15:18
  • @CoolCloud how do you do that? Commented Mar 6, 2021 at 2:04
  • Please take a read here. Commented Mar 6, 2021 at 6:09

1 Answer 1

2

Solution:

I am not sure what you where trying to do using == but it should be:

messagebox.showinfo(f"The top words are: {n_print}")

Or even:

messagebox.showinfo("The top words are: "+n_print)

Reason:

The mistake is that when you say "The top words are: " == n_print it means you are comparing a string with n_print and it will return True or False. So you're asking messagebox to display False which is a bool and it fails to display bools and hence you are getting an empty messagebox. See:

[In] : "The top words are: " == n_print # Assuming n_print = 'hey'
[Out]: False

As long as n_print is "The top words are: ", it will always return False.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.