0

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.

Ant
  • 1,668
  • 2
  • 18
  • 35
  • If I understand you correctly, you would like to make a widget disappear on command. This has been answered here - https://stackoverflow.com/questions/3819354/in-tkinter-is-there-any-way-to-make-a-widget-not-visible – flevinkelming Jul 28 '17 at 15:11
  • You can probably use tkinters `toplevel` windows for this. The use of toplevel is to open separate windows on top of tkinters main root window that can then be used for anything you would do in tkinter. – Mike - SMT Jul 28 '17 at 15:11
  • You can use `.update_idletasks` / `.update` instead of `.mainloop`. This script should give you some ideas: [A Tkinter viewer for named PIL Images](https://gist.github.com/PM2Ring/467cf3d1e884ea508efd996f95fd871e). Another approach is to use threading, although it can be tricky to make Tkinter work properly with threads. – PM 2Ring Jul 28 '17 at 16:05
  • @flevinkelming It was the destroy rather than hide that I was looking for. – Ant Jul 28 '17 at 16:09
  • @SierraMountainTech The point was that I have a script and only want any window for that one task. So there is no main root window. – Ant Jul 28 '17 at 16:10

1 Answers1

1

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")
James Kent
  • 5,763
  • 26
  • 50
  • That is exactly what I needed, thank you. I think the part I was missing was the destroy. – Ant Jul 28 '17 at 16:08
  • not a problem, and fyi, the `destroy method` is also called if you close the window using the X so you get the same behaviour either way – James Kent Jul 28 '17 at 16:30