Reputation: 1708
I need help understanding the interaction between a Python script and tkinter. I am fairly sure that this question will already have been answered but I can't find it so I expect my search terms are wrong.
I have developed a Python script that I use daily that prints information as text and takes text input. There is one place where I need to display a lot of information in tabular form and I'm trying to use tkinter for that job. So I would like a window / dialog to appear to show the information and once an OK button is pressed for it to disappear again.
All the examples of using tkinter seem to enter the tkinter mainloop at the end of the script which I think means all data in and out is through tkinter.
The best I've achieved so far opens a window with all the data well presented and the button ends the script completely.
exitBut = tkinter.Button(frameButtons, text="Exit", command=exitFn)
exitBut.grid(row=0, column=0)
describeDialog.mainloop()
Can you help me understand how to create and destroy a temporary tkinter window in the middle of a Python script please.
Upvotes: 0
Views: 993
Reputation: 5933
a simple example here, maybe not best practice but shows that you can write your script as you normally would without using tkinter, show it with tkinter and then carry on without it.
import tkinter as tk
print("pretending to do something here")
root=tk.Tk()
root.title("my window")
tk.Label(root, text="show something here").pack()
tk.Button(root, text="ok", command=root.destroy).pack()
root.mainloop()
print("now we can do something else")
print("note how execution of script stops at the call to mainloop")
print("but resumes afterwards")
Upvotes: 1