Facing problem with overrideredirect(True) method

2020-08-02 03:43发布

问题:

I am developing an GUI using python tkinter in debian based linux.

Whenever I am removing root and toplevel window title bar using overrideredirect(True) method I am facing below mentioned problems.

  1. Entry field not working(not getting focus/not getting input) when I am clicking on it.
  2. Whenever I use Alt+Tab key then only it get focused.
  3. Then I have to keep pressed mouse left or right button on clicked position then only entry field accept input otherwise input is written on terminal window.

Could any one suggest an alternative way to remove titlebar or to hide title bar in tkinter on linux based system.This code runs well on windows system.

There is a question Tkinter's overrideredirect prevents certain events in Mac and Linux which seems same as my problem but I could not getting its solution .

from tkinter import *

class GuiApp:
    def __init__(self,master):
        master_frame = Frame(master,background= 'blue')
        master_frame.grid()
        b1 = Button(master_frame,
                      height=3,text="BUTTON",width=15, font=14,command=lambda parent=master: self.create_top_window(parent))
        b1.grid(row=0, column=0, pady=3, padx=5)
        master_frame.grab_set()

    def create_top_window(self,parent):
        toplevel = Toplevel(parent, bg='red', bd=5, relief=RIDGE)
        toplevel.geometry('350x250')
        toplevel.wm_overrideredirect(True)
#       toplevel.wm_attributes('-type','splash')
        label=Label(toplevel,text='Entry:')
        entry=Entry(toplevel,width=10,font=13)
        button = Button(toplevel, text='close', font=('TkTextFont', 14), command=toplevel.destroy)
        label.grid(row=0,column=0,padx=5,pady=5)
        entry.grid(row=0,column=1,padx=5,pady=5)
        button.grid(row=1, column=0, padx=1, pady=1)
        toplevel.grab_set()

root = Tk()
root.wm_overrideredirect(True)
#root.wm_attributes('-type','splash')
app = GuiApp(root)
root.mainloop()

回答1:

The titlebar is provided by the window manager. When you set the overrideredirect flag you are asking the window manager to ignore your window which doesn't just remove the decorations. However, the Extended Window Manager Hints protocol allows you to specify the intended type of a window (menu, splash, dialog etc) and offers a way to hint to the window manager so that it can provided suitable decoration. In this case it sounds like a splash screen so you might try using "splash" as a window type:

self.wm_attributes('-type', 'splash')

The 'type' attribute is only provided when the Tk windowing system is 'x11'. The splash hint should get rid of the window manager decoration for you although this depends on the window manager configuration.



标签: tk