Tkinter entry for variable

Viewed 45

i've red a lot of post here and i couldn't find a clue on my problem even if it was repeated a lot, it doesn't really apply to my case.

My code is simple. I have a string, i want to create the wordcloud of this string, but i also created a function that gets in entry an integer, and returns the wordcloud with the number of words in the variable. My code works well alone, my function is called "test_wordcloud". If i enter "test_wordcloud(4)" for example, it will return the wordcloud with the 4 most present words in the string.

def test_wordcloud(n): 
    if type(n) == "int":
        wordcloud = WordCloud(width=900, height=400, background_color="white", max_font_size=125,
                          min_font_size=20, max_words=n).generate(string) 
        fig = plt.figure(figsize = (10, 10))
        plt.axis("off")
        plt.imshow(wordcloud)
        fig.savefig("Test Tkinter.jpg", bbox_inches='tight', dpi=150)
        source_path = "C:\\Users\\f002722\\Stage NLP\\Test Tkinter.jpg"
        destination_path = "C:\\Users\\f002722\\Desktop\\NLP4DTV 2.0\\results\\data\\Test Tkinter.jpg"
        shutil.move(source_path, destination_path)
        return "DONE"

Now i want to make a Tkinter interface where the user enter the number of words he wants, then the wordcloud in format jpg is created in the folder up there and he can see it. But it doesn't work. So far i've done like this.

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo

root = tk.Tk()
root.title("WordCloud configuration")
title = root.title()

window_width = 300
window_height = 300

# Center the window
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')

wordnumber = tk.StringVar()
wordnumberlabel = ttk.Label(root, text = "Number of words").pack() 
entry = ttk.Entry(root, textvariable = wordnumber) 
entry.pack()
ttk.Button(root, text = "Ok", command = test_wordcloud(entry.get())).pack()

root.mainloop()

I really don't see where i'm wrong, my command on the button is "test_wordcloud(entry.get())" which should guarantee my code to run this function with the entry, but when i do so, nothing happens. The window opens with the button and the place where to right the variable but when i press "ok" nothing happens.

I woudl really appreciate help her because i keep looking for answer on google but i feel stuck, thx in advance

0 Answers
Related