How to clear a tkinter window Python? - python

I have tried to clear some widgets (buttons) from a tkinter window in python by using button.destroy() but I have to use the button after so I have tried button.pack_forget().
After using the forget method I have tried to use the button again by doing button.pack() but it's not working.
Why?

Related

Tkinter dialog box with user input AND custom buttons

Can't find this anywhere else on stack overflow.
I want to create a dialog box (on top of my main window) in tkinter that has message, a text input field and some custom buttons.
I know I can do
import tkinter as tk
from tkinter import simpledialog
master = tk.Tk()
user_input = tk.simpledialog.askstring("title", "message")
and then take the user_input from there. However, how can I make the buttons have custom text and also bind them to my own functions?
Otherwise, I was thinking of making my own dialogbox class. But that's a lot of work. Is there any way I can do this natively using any of tkinter' modules?
Regards
Hugo.

Module naming overlaps

I'm using Tkinter with a GUI using a button inside the root window.
I also want to use a Button inside a MatplotLib graph that allows me to exit the graph (and potentially for other uses in the future).
The only problem is that they both use the same label Button. These two labels have different syntax so I can only either have Tkinter buttons or Matplotlib buttons.
I'm sure this is a very amateur question but is there a way to specify that this Button is a tkinter button while another Button is a matplotlib button?
Here's an example of the code:
from matplotlib.widgets import Slider, Button, RadioButtons
from tkinter import *
btn = Button(root, text="Plot", command=graph).pack()
Found the answer but this might be helpful to some people
You can instead use
import tkinter as tk
and then everytime you want to you a tkinter function you specify it by writing "tk." before it.
E.g.:
tk.Button
in this case and it will specifically use the tkinter version of the Button

Python mouse event

I am trying to activate a function in python which will save a timestamp to a file. However, I wish to activate that function after the first mouse click that accurses outside the Tk window.
For example, after running the program I will minimize it and press on the chrome browser icon and while doing so my program will log the time stamp.
I tried to use the bind function but it works for clicks that accrue only in the Tk window
By the way, I am currently using Tkinter as my GUI platform however if there is a way to do it with another libraries please share and I will adjust my program
thanks :)
Use the <FocusOut> event:
import tkinter as tk
root = tk.Tk()
def focus_lost(event):
print("Clicked outside the window")
root.bind("<FocusOut>", focus_lost)
root.mainloop()
If you are focussed on the tkinter window (e.g. if you have clicked on it), then when you click elsewhere it will detect this as a <FocusOut> event.

How to use tkinter to show information in the middle of a script

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.
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")

can Python Tkinter widget show a thread?

Is there a widget that shows a thread visually for Tkinter?
reason why. I want to open a Vpython window inside a Tkinter window.
I know there is a possibility to open a Vpython thread on the side while Tkinter is active, but can i show it inside Tkinter? (Like in some sort of Frame)
No, there is no widget to do that.

Categories