问题
Is there a way to create non-standard windows with Tkinter? I want something of a floating image on the screen, with Tkinter widgets inside. Think something like Growl for Mac, Siri on the iPad, Mac OS volume/brightness change bezels, etc. If this isn't possible, is there a way to get rid of the top bar on the window with the title and close/minimize/resize buttons, and require the script to complete (or modifier-Q keystroke) to close?
回答1:
wm_overrideredirect will remove the standard window borders. You'll still be stuck with a rectangular window. You can adjust the transparency of the window with wm_attributes (look for the alpha
attribute), though this only works on Windows and the Mac.
There have been attempts at shaped windows with tcl/tk, which you might be able to get to work with Tkinter, though it requires compiling some code. See Managed and shaped toplevel on the tcl'ers wiki.
回答2:
yeah you can! you can use
app.overrideredirect(True)
where app = Tk()
from here you are stuck with a window that is not top level though; it also DOES NOT have a taskbar position;
to fix this we can use:
app.attributes('-topmost', 1)
but even now you have just a rectangle; you will need to create a custom title bar; this is just a frame with a title, exit, minimize button, etc.
To move the window from the title bar we use:
def get_pos(event):
xwin = app.winfo_x()
ywin = app.winfo_y()
startx = event.x_root
starty = event.y_root
ywin = ywin - starty
xwin = xwin - startx
def move_window(event):
app.geometry("400x400" + '+{0}+{1}'.format(event.x_root + xwin, event.y_root + ywin))
startx = event.x_root
starty = event.y_root
app.TopFrame.bind('<B1-Motion>', move_window)
app.TopFrame.bind('<Button-1>', get_pos)
where TopFrame is the title bar. This method seems over complicated, however it allows for the bar to move directly on the mouse without shifting to the corner.
answering your latter question: Making a floating object is highly possible; use:
app.overrideredirect(True)
app.image = tk.PhotoImage(file='image.GIF')
image = Label(root, image=app.image, bg='white')
app.attributes("-transparentcolor", "white")
来源:https://stackoverflow.com/questions/12605003/non-standard-windows-with-tkinter