Close undecorated frames tkinter python - python

I am making a UI using tkinter in python. I want to make a full screen window for my UI, without any titles or border like an undecorated frame in Java. As described on http://effbot.org/tkinterbook/wm.htm I used
root.overrideredirect(True)
It works fine and I have an full screen window, but I can't close this window using normal close operation like ALt+F4 unlike undecorated frames in Java. I need to add a functionality to close my overrideredirect window using keyboard Alt+F4 keys. How can I do that?
If not possible to add that functionality, can I use an another way to make a full screen window using any other command such that I have close with Alt+F4 keys functionality

For me Alt+F4 works fine for an undecorated window, so i'm not sure what's going wrong for you.
That being said, you can of course make a button which destroys the window. This is probably best any way, since not everyone is familiar with (simple) keyboard shortcuts.
self.button_quit = Tk.Button(self.root, text='Quit', command=self.quit).pack
def quit(self):
self.root.destroy()

Related

how to make tkinter window not moveable with the mouse

very simple problem, I just try to make tkinter window that you are not able to move with the mouse. the problem is that I don't find a function that can do it, the only thing that I found is making it not resizable and stuff like that, I also found a function that hides the bar and making it not resizable or moveable, but it doesn't help me cause I still want it to have the default window bar, let's just say I have a normal window here:
import tkinter as TK
window = TK.Tk()
window.geometry('1200x800')
window.mainloop()
so just tell me if you know how to make it impossible to move the window with the mouse, how to do that, the code should work fine with fullscreen and other functionality like that by the way.

How to close tkinter window without a button and not closing Python completely?

I am aware that there are commands such as .destroy() , .exit() etc. However, when taking these out of the 'command' from the button parameter of what action to take when pressed, they don't work.
My scenario is when a user logins successfully, the Tkinter window including its widgets should close whilst short after a GUI in pygame opens. I just don't want the Tkinter window to be there once I don't need it ever again whilst also not exiting Python. I don't want a button as I want the process to be automatic.
The thing that baffles me is why when taking out this command to be on its own, it does not work:
Button(root, text="Quit", command=root.destroy).pack() #works
root.destroy() #don't works
Without seeing more of the source code, I guess the problem is based on where you call root.destroy()
If it comes after the blocking tk.mainloop(), it will never be reached. Please read Tkinter understanding mainloop regarding this issue.
A possible solution based on the above:
while True:
tk.update_idletasks()
tk.update()
if login_successful: # or whatever your login check looks like
root.destroy()
You replace the mainloop with your custom loop, which includes a check for a successful login.

Python tkinter keeping window on top when focusing on another window

I'm making a text editor, and I have made a find/replace function with it's own window.
It looks like this. I want it to become transparent when changing back to the main text window, but stay there. (Like what notepad++ does).
As you can see here, the find window is in front of the main window but it is transparent.
I already know I can use this to make the window transparent:
find_window.attributes('-alpha', 0.3)
But now I want it to stay on top while the focus is on the other window.
I have figured it out myself. I had to use the focus_out event and then use .lift() to place it on top:
def leave():
find_window.attributes('-alpha', 0.3)
find_window.lift()
find_window.bind('<FocusOut>', leave)

How should I make a class that can be used as my main app window, but also can be used as a secondary window

I know that I can subclass a tk.Frame (or ttk.Frame) and add that to a TopLevel to make secondary windows, but I'm not sure how I should use that as the main window. I know that creating an instance of a Frame class and calling .mainloop() on it seems to work for using it as the main window, but I feel like that's bad practice...
What do other people do when they are making GUI layouts that they want to have available to main windows and secondary windows?
Create a subclass of a Frame, and then put it either in the root window or a toplevel. In either case, you still call mainloop only once on the root window.
The only care you have to take is that you have to be careful about letting the user close the root window, because it will cause all of the other windows to be destroyed.
If you're creating a program that can have multiple windows, you might want to consider hiding the root window and always putting your window in a Toplevel. Of course, when you do that you need to make sure you destroy the root window whenever the last toplevel window is destroyed, or your program will continue to run but the user will have no way to access it.
Do you mean having a home screen that you can flip back to? If so, you can try looking here: Using buttons in Tkinter to navigate to different pages of the application?

tkinter.mainloop does not work

I am trying to create a basic Tkinter window.
According to on-line tutorials, to create a window one must use the following:
import Tkinter
window=Tkinter.Tk()
window.mainloop()
But when I try the same code python directly displays the window in window=Tkinter.Tk() and window.mainloop() has no effect.
Can anyone explain why ?
EDIT: The code works perfectly when I put it in a file and run it. It just doesn't work from interactive prompt.
The call to mainloop is there so that you can interact with the Window once it's created. If you had a Python script that only did this:
import Tkinter
window = Tkinter.Tk()
The script would exit immediately after window was created, so you'd be luckily to even see it get drawn before it disappeared as the script exited. (That is if window was even drawn at all; in my tests on both Linux and Windows, window was never drawn unless mainloop was called; even if I put a call to time.sleep after the Tkinter.Tk() call, window would only be drawn without a mainloop call in the interactive prompt).
The mainloop() also (and most importantly) allows Tkinter to listen for events to occur on the Tk object, such as pressing buttons, radios, etc. that might be embedded in it, and dispatch those events to methods you have bound to the event being triggered. Without that functionality you'd just have a window that you can look at and not much else.

Categories

Resources