Remove titlebar without overrideredirect() using Tkinter? - python

I'm currently working with Tkinter and Python 2.7 on Linux and I was wondering if there was a way to remove the TK() window border frame and title bar without using overrideredirect(1).
I have my own close button and overrideredirect(1) presents me with a few issues that I can't accept:
GUI always on top
can't iconify then deiconify properly
no keyboard input so can't type into fields (see python tkinter overrideredirect; cannot receive keystrokes (Linux))
I can't use attributes("-fullscreen", True) as the titlebar and borders remain.

The window decoration is all handled by the window manager so what you are trying to do is find a way to tell the window manager to decorate your window differently from a standard application window. Tk provides overrideredirect to have the window manager completely ignore this window but we can also use Extended Window Manager Hints to declare the intended use of this toplevel window to the window manager. This is done for instance for tooltip and splashscreen windows to allow the manager to provide minimal decoration and possibly special animations.
In your case, adding a 'splash' hint should do what you want
root = tk.Tk()
root.wm_attributes('-type', 'splash')
You will need Tk 8.5 or above for this.

You must give your root window name before your command.
Like this:
from tkinter import *
root=Tk()
root.wm_attributes('-fullscreen','true')
root.mainloop()

Related

tkinter fullscreen still available after root.resizable(False,False)

I am trying to make a python application that requires disabling the window resize and fullscreen mode on a Mac. I used root.resizable(False,False) to do the work, it does stop the window from resizing. But for disabling the fullscreen mode, only if you iconify it first and then deiconify it. I wonder why and is there a workaround?
According to the tk documentation, this is probably a bug in the window manager. Keep in mind that resizable is a wm command.
Most existing window managers appear to have bugs that affect the operation of the wm command. For example, some changes will not take effect if the window is already active: the window will have to be withdrawn and de-iconified in order to make the change happen.

how to change a window border color in tkinter

I'm having a problem with changing the background of this part of the window:
How do I change it?
As I see you are using windows.
This color is set by the theme you are currently using. It is the same for every window.
So I cross out the possibility of only using the Tkinter module for this.
Tkinter is responsible for what is in the window but the window manager decides about the border. For example in Ubuntu the window would look totally different.
I guess, you would need some windows specific calls for that.
You can remove the border with root.overrideredirect(1) if I remember correctly.
PS: put "windows" into the tags of this question.

Is it bad practice to create a second tkinter Tk() after the first instance has been destroyed by closing the window?

I am learning about Tkinter and was wondering if it would cause errors if I did the following:
import tkinter as tk #import modules
from tkinter import ttk
parent=tk.Tk() #create first instance
card1="k spades"
card2="k diamonds"
comboform=ttk.Combobox(parent,textvariable='form',values=[card1,card2,"both","neither"])#create combobox input form
comboform.grid(row=0,column=0)#added to grid
parent.geometry("200x200")
parent.mainloop()#displays tkinter window
#window exited
parent=tk.Tk()#new instance created
label=tk.Label(parent,text="hi")#label produced
label.pack()#added to window
parent.mainloop()
If I click the exit cross is that the same as parent.destroy(); is that good practice? I know you're only supposed to run mainloop() once and have one Tk() instance but if it's destroyed is it going to cause problems? It's not like I'm creating a class the produces a Tk() instance, where there's a risk of multiple instances existing at once.
I am hoping to, eventually, have an application running in the IDLE and then have a tkinter window appear, presenting an input widget of some kind. After the user gives their input, the window would close and the user would continue in the main window. But could I then do it again, opening new windows (like the above code) on the provision that the instance of Tk() is destroyed each time?
If you've destroyed the root window and then create a new one, that's perfectly fine.
The problem with creating multiple instances of Tk is that most people don't understand what that actually does. Having multiple instances of Tk is fine as long as you realize that they operate in completely memory spaces and widgets and bindings in one can't interact with widgets and bindings in the other.
All of that being said, the best practice is to create a single root window at the start of the program, and it stays alive until the program exits. If you need additional windows, the best practice is to create instances of Toplevel.

Tkinter window with both title bar and windows taskbar

I have searched wide and far for this question, but noone seems to know. I create a simple tkinter window (tcl 8.5) in python 2.7 and want it maximized, just as if I would hit the maximize button in top right corner. Using the -fullscreen option is not an option since it removes the title bar.
I tried the following:
import Tkinter
root = Tkinter.Tk()
root.overrideredirect(True)
# Set window to be size of screen
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
The problem is that the window is now below the Windows taskbar and some of my elements are therefore not shown. An easy hack would be to set height to screenheight-some_constant, or calculate some_constant based on data from the operating system. However, this seems like an extremely ugly hack.
Is there any way to maximize a window in tkinter in a clean way where the window is above the (Windows) taskbar and still has a title bar?
i know this is an old question but i have tested the following code on an XP system (32bit) with python 2.7.6, using TCL version 8.5 and tk version 8.5.
code:
import Tkinter as tk
root = tk.Tk()
root.state("zoomed")
root.mainloop()
this does start the window maximised on the primary monitor.
it does have pitfalls - i have been unable to make it maximise on another monitor, but does work reliably without covering the title bar or taskbar.
and as you want to keep the title bar i would leave out the overrideredirect.

'hover over' popup with Tkinter

I have implemented an informational popup in a python app using a Tkinter Menu widget. I have a Text widget on a canvas in the root window. I created a Menu widget that has root as its parent. When I detect a mouse hover over the text widget I post the popup menu with menuWidget.post(). When I get a leave event from the text widget my intention was to have the popup disappear by calling menuWidget.unpost(), only the popup menu does not disappear until I click elsewhere outside the text widget.
First, is this a sane method for implementing an informational popup? And can anyone tell me why the popup menu won't disappear?
This is not the right way to do an informational popup. On the Mac and on windows machines menus are native controls. Because of this the unpost command doesn't work because tk cedes control to the system event loop in order to get platform-specific behavior.
What you want is to use instead is a toplevel window with the overrideredirect flag set. This lets you display a borderless window anywhere you want. The upside to this is that you aren't limited to simple text -- you can put anything you want in that toplevel -- another text widget, a canvas, buttons, etc.

Categories