Reputation: 63
I am creating a function in which I output a text file's contents to a 'Message' widget in Tkinter. The user selects an option which corresponds to a text file, and presses 'OK'.
The problem I'm having is that I do not know how to clear the message box after selecting two consecutive options.
QUESTION: How do I clear the message box of the first text, before outputting the second? At the moment the second text outputs over the top of the first.
I have tried Message.delete and Message.clear but I don't think they're applicable to the message widget.
Any help would be appreciated.
This is my code:
def learn(event):
''' This function creates a new window within the main window, passes an event(left mouse click), and creates a text heading'''
root = Toplevel(window)
menu_choice = StringVar(root)
menu_choice.set("Select") # initial value
selection_message = Message(root, text = "Choose which area of finances you'd like to learn about below!", width = 180)
selection_message.grid(row = 0, column = 0, columnspan = 6)
menu_options = OptionMenu(root, menu_choice, "Stocks", "Bonds", "Index Funds", "Exchange Traded Funds (ETF's)")
menu_options.grid(row = 1, column = 2)
def selection():
learn_area = menu_choice.get()
learn_file = open('C:\\Users\\nicks_000\\PycharmProjects\\untitled\\SAT\\GUI\\Text Files\\{0}.txt'.format(learn_area))
learn_text = learn_file.read()
learn_file.close()
about_message = Message(root, text = learn_text, width = 300, relief = RAISED)
about_message.grid(row = 3, column = 0, columnspan = 6)
selection_button = Button(root, text="OK", command=selection)
selection_button.grid(row = 2, column = 2)
Upvotes: 1
Views: 2632
Reputation: 16179
Your problem is that you created the about_message
widget inside the selection function, so you recreate one each time you call the function. I suggest you to create the widget outside the selection function so that you can do about_message.configure(text="new text")
. Here is the code:
from tkinter import *
window = Tk()
def learn():
''' This function creates a new window within the main window, passes an event(left mouse click), and creates a text heading'''
root = Toplevel(window)
menu_choice = StringVar(root)
menu_choice.set("Select") # initial value
selection_message = Message(root, text = "Choose which area of finances you'd like to learn about below!", width = 180)
selection_message.grid(row = 0, column = 0, columnspan = 6)
menu_options = OptionMenu(root, menu_choice, "Stocks", "Bonds", "Index Funds", "Exchange Traded Funds (ETF's)")
menu_options.grid(row = 1, column = 2)
def selection():
learn_area = menu_choice.get()
learn_file = open('C:\\Users\\nicks_000\\PycharmProjects\\untitled\\SAT\\GUI\\Text Files\\{0}.txt'.format(learn_area))
learn_text = learn_file.read()
learn_file.close()
about_message.configure(text=learn_text)
selection_button = Button(root, text="OK", command=selection)
selection_button.grid(row = 2, column = 2)
# create about_message outside the selection function
# to be able to modify its content
about_message = Message(root, text = "", width = 300, relief = RAISED)
about_message.grid(row = 3, column = 0, columnspan = 6)
Button(window,text="test", command=learn).pack()
window.mainloop()
Upvotes: 1