Closing Python Tkinter window mainloop without a button - python

I would like to refresh a python Tkinter window every 1 seconds :
animation=TFS(test) #animation is a list, each item in the list is a table with drawing input information.
for item in animation:
Display(item) #display function is creating a Tkinter window with a mainloop
time.sleep(1)
The only solution about closing a mainloop for Tkinter that i found on the internet, is by using .quit() or .destroy() in a button.
But I don't want the user to close the window manually, the window should be closed automatically every x second in order to be able to display a new window with updated information.
Note : the display is made of about 2 hundreds rectangles and labels.

The .destroy() method can be called anywhere in the code, not necessarily when button is pressed. To close the window, it has to be called on Tk object (main window, the first one you created). But, like #Billal BEGUERADJ said, you don't need to close the window to refresh widgets inside it. Just call .config() method on the widget you'd like to modify and pass options you'd like to change as arguments.

The display in the canva is procedural with hundreds of shapes, so the .configure seems hard to used.
So i deleted the .mainloop() in the function Display and replaced it by :
TK.update()
time.sleep(RefreshTime)
TK.destroy()
return
and changed the main code to the following :
animation=TFS(test)
for step in animation:
Display.display(step,RefreshTime)

Related

Drag and Drop button tkinter python

I'm new using tkinter on python and I would like to develop a program that can Drag and Drop a button pressing other one... I will try to explain : I have button 'A' that will create a new button 'B' and I want to Drag the New button to another place
Any help
Thanks
The tkinter.dnd module, as suggested by j_4321 in comments.
Here is some sample code using that library to do what you have said:
from tkinter import *
from tkinter.dnd import Tester as DragWindow, Icon as Dragable
# Make a root window and hide it, since we don't need it.
root = Tk()
root.withdraw()
# Make the actual main window, which can have dragable objects on.
main = DragWindow(root)
def make_btn():
"""Make a new test button."""
# The functional part of the main window is the canvas.
Dragable('B').attach(main.canvas)
# Make a button and bind it to our button creating function.
Button(main.top, text='A', command=make_btn).pack()
# Start the mainloop.
mainloop()

python tkinter Toplevel .destroy() vs .quit() not working as intended

I have a class Duplicates that checks for duplicates within 40 words.
I have a class Window that creates and runs the main window where i post the result.
I have a class popWindow that creates a Toplevel window when asking user for what to do with a possible double.
My problem is closing the popWindow once a choice is submited.
the version I have that actualy runs and posts an aswer (the text with marked duplicates) uses quit to terminate the window (meaning the popup is still there in the way) or to simply have multiple popups till you are done.
class Duplicates:
def markWord(self):
self.appendMarkedWord(self.word)
self.checked.append(self.word)
self.pop.topLevel_exit()
return ""
class popUpWindow:
temp = Button( self, font = 8,
text = "Allowed this run only",
command = app.newFile.markWord
)
temp.place( x = 178,
y = 55
)
if I instead use .destroy() the window shuts but the program stops running and that is worse.
How do i work around this so it shuts the window but still continues to run the program?
Ok, after many many hours it seemed the real problem was destroy() was not stopping my popUpWindow.mainloop() so I now have altered my exit code to first do quit() and then do destroy(). This is not what i have seen as examples at all and it seems to me that destroy() on toplevel mainloop is not terminating it (destroy() works fine on my root.mainloop).
def topLevel_exit(self):
self.pop.quit()
self.pop.destroy()
If you call destroy() on a toplevel window, it will not stop the application from running. If your application stops, there must be more to your code that what you're telling us. Without question, the right way to get rid of the popup is to call destroy on the instance of the Toplevel.
A way to hide the window and keep the program running would be to use .withdraw() on the window, and .reiconify() to get it back (if needed). Or you could use .destroy() on a Toplevel window. If you need examples just ask, hope this helps you.
The solution for me was:
def topLevel_exit(self):
self.top.quit()
self.top.destroy()
I do not know if this is common praxis but is what I had to do since destroy was not stoping my top.mainloop()
If you use a topLevel window, self.pop.destroy() should still work as you are using mainloop()
Otherwise use quit() or both but in my opinion of all of these, I prefer destroy()

Tkinter overrideredirect no longer receiving event bindings

I have a tinter Toplevel window that I want to come up without a frame or a titlebar and slightly transparent, and then solid when the mouse moves over the window. To do this I am using both Toplevel.overrideredirect(True) and Toplevel.attributes('-alpha', 0.75). I am binding the <Enter> and <Leave> events to a function for this.
These all work when tried separately, but when I have the overrideredirect set to True, the bindings for the mouse entering and leaving no longer works. The binding calls when I click on the window, and then when I move the mouse, but not when the curser enter or leave the window.
I have also tried binding these to a Frame, but with no further luck.
toplevel = Toplevel(root)
toplevel.overrideredirect(True)
toplevel.attributes('-alpha', 0.75)
toplevel.bind('<Enter>', lambda x: mouseMovement(command='enter'))
toplevel.bind('<Leave>', lambda x: mouseMovement(command='leave'))
def mouseMovement(command):
print('Callback: ' + command)
if command == 'enter':
toplevel.attributes('-alpha', 1)
elif command == 'leave':
toplevel.attributes('-alpha', 0.75)
I have tried using the answer to the similar question here, but this results in a window that has all the standard OS decorations, but the close, minimise, and enlarge buttons are simply disabled. Is there a way where I can get rid of the titlebar, but still keep my bindings?
On X Windows this can be handled using appropriate Extended Window Manager Hints to request the window manager to decorate the toplevel the way desired. This sounds like a splash screen window so 'splash' is likely to be appropriate here. For this use the wm_attributes -type parameter eg:
toplevel.wm_attributes('-type', 'splash')
will have the toplevel decorated as for a splash screen dialog which usually means no title bar. If you apply this to an already mapped window, you will need to withdraw and re-map (call wm_deiconify) to get the window manager to apply its settings for the new hint type.

Automatically close window after a certain time

In a class, in a function I am creating a Tkinter Canvas. This function is being called by another class, I would like for the Tkinter window to pop up for 30 seconds and then close itself. I have it call
master.mainloop()
time.sleep(30)
master.destroy()
But I get an error
"elf.tk.call('destroy', self._w)
_tkinter.TclError: can't invoke "destroy" command: application has been destroyed"
So how can I have it close itself?
Don't use time.sleep() with tkinter. Instead, call the function after on the widget you want to close.
Here it is the most simple example:
import tkinter as tk
w = tk.Tk()
w.after(30000, lambda: w.destroy()) # Destroy the widget after 30 seconds
w.mainloop()
The problem here is that mainloop() does not return until the GUI has shut down.
So, 30 seconds after the GUI has shut down and destroyed itself, you try to destroy it. And obviously that fails.
But you can't just move the sleep and destroy calls inside the main loop, because if you sleep in the middle of the main loop, the GUI will freeze up.
So, you need some kind of timer that will not stop the main loop. tkinter includes the after method for exactly that purpose. This answer gives a detailed example of using it.

Remove window title bar in TKinter Python

How do I remove the title bar from a Toplevel() window in Tkinter.
Right now I for my main I have
self.master.title("Subtest")
self.master.geometry("400x200")
self.alertwindow()
Label(self.master,textvariable=self.connected,height=4).grid(row=0,column=0)
Button(self.master,text="Monitor",command= lambda: self.startnewthread(1),width=10).grid(row=6,column=1)
Button(self.master,text="Quit",command=self.haltprogram).grid(row=6,column=0)
And for my alert window function I have
def alertwindow(self):
self.listbox=Listbox(Toplevel(self.master,width=150).overrideredirect(True),width=150).pack)
I was wanting the program to open up a root window, and then a toplevel listbox without a title bar; however, the only thing the program is doing right now is freezing, and when I remove the .overrideredirect(True), the program launches two listbox windows. How can I have the program open only one listbox without a title bar on windows? Thanks
Looking at this line
self.listbox=Listbox(Toplevel(self.master,width=150).overrideredirect(True),width=150).pack)
It's pretty clear you're trying to do WAY too much on 1 line. (Your parenthesis don't even match). Let's break it up, shall we?
new_top = Toplevel(self.master,width=150)
new_top.overrideredirect(True)
self.listbox = Listbox(new_top,width=150)
self.listbox.pack()
Also note that you seem to be using .grid and .pack -- Generally that's ill advised and Tkinter will happily spend all of eternity trying to negotiate a proper placement of a widget when you try to use them together.
My guess about what's happening:
your actual code has properly balanced parenthesis so there is no SyntaxError
Toplevel.overrideredirct returns None
Listbox sees None as the parent widget and substitutes the root widget (Tk)
Then you're using .grid and .pack both on the root widget which causes your program to hang.

Categories