I try to make a splash screen with a png.
I use Python 3.7.4 64-bit, under macOS 10.14.4 and Visual Code 1.33.1
With root.overrideredirect(True) no windows are displayed.
With root.overrideredirect(False) the png is correctly displayed but the top window border is visible.
import tkinter as tk
root = tk.Tk()
# Hide the root window drag bar and close button
root.overrideredirect(True)
# Make the root window always on top
root.wm_attributes('-topmost', True)
# Turn off the window shadow
root.wm_attributes('-transparent', True)
# Set the root window background color to a transparent color
root.config(bg='systemTransparent')
root.geometry('+300+300')
# Store the PhotoImage to prevent early garbage collection
root.image = tk.PhotoImage(file='./local/pics/splash.png')
# Display the image on a label
label = tk.Label(root, image=root.image)
# Set the label background color to a transparent color
label.config(bg='systemTransparent')
label.pack()
root.mainloop()
Thanks for your help
This will hide the title bar
and fixates your window to top always:
root.attributes('-type', 'dock')
If you don't need it at the top:
root.attributes('-type', 'splash')
This will hide the title bar
If this can be improved please comment.
Related
I'm trying to display an image that already has a transparent background into a window. Currently I'm using OpenCV cv2.imshow which doesn't show the alpha channel and that results in the pixels being black. Are there any other library or different kinds of approach that shows an image with a transparent background in a window with the background desktop screen showing?
Original Image:
Current result:
Desired result:
You can use the python standard library Tkinter to show an image in a transparent window.
Code Snippet:
from tkinter import Tk, Canvas, PhotoImage, NW
root = Tk()
root.attributes('-transparentcolor','#f0f0f0')
# Canvas
canvas = Canvas(root, width=450, height=600)
canvas.pack()
# Image
img = PhotoImage(file="./images/panda.png")
# Positioning the Image inside the canvas
canvas.create_image(0, 0, anchor=NW, image=img)
# Starts the GUI
root.mainloop()
I want to set my root window transparent, I have done this easily, but I can't move anything in the background of my system, for example my terminal, or anything else.
from tkinter import Tk
self.tk = Tk()
self.tk.attributes('-zoomed', True) # This just maximizes the window
self.tk.wait_visibility() # just to fix self.tk.attributes
self.tk.attributes('-type', 'dock') # disable title and title buttons
self.tk.attributes('-alpha', 0.1) # transparent
self.tk.mainloop() # main loop
The reason I want to do that, is because I want to make an screenshot application, and I want to make some effects that make the system transparent
You could make any color completly transparent in your window.
from tkinter import Tk
root = Tk()
root.configure(bg='white')
root.attributes('-transparentcolor', 'white')
root.attributes('-topmost', True)
root.mainloop()
I think code speaks for itself. (OS dependent, works on windows)
I am making a chess program and I want to be able to drag the pieces. In order to do this, I put the image of the piece on a Canvas so it can be dragged (I can also use a Label if I want). However, when I drag the piece there is a white square that surrounds the image of the piece.
When I researched the problem, many people gave this solution:
drag_canvas = Canvas(self, height=80, width=80, bg="yellow")
root.wm_attributes("-transparentcolor", "yellow")
This caused the background to be transparent but it was not the chessboard that was visible, it was the program behind the GUI
.
Is there any way I can have the background be transparent and show the chessboard behind rather than the program behind the tkinter window?
Note: I do not mind using any other widget (e.g. a Label) but they must use modules that come default with Python (so no PIL) as this program needs to be used in an environment where I cannot download other modules.
Question: How to make a tkinter canvas background transparent?
The only possible config(... option, to set the background to nothing
c.config(bg='')
results with: _tkinter.TclError: unknown color name ""
To get this result:
you have to hold the chess board and figures within the same .Canvas(....
self.canvas = Canvas(self, width=500, height=200, bd=0, highlightthickness=0)
self.canvas.create_rectangle(245,50,345,150, fill='white')
self.image = tk.PhotoImage(file='chess.png')
self.image_id = self.canvas.create_image(50,50, image=self.image)
self.canvas.move(self.image_id, 245, 100)
Tested with Python: 3.5 - TkVersion: 8.6
A windows only solution is to use the pywin32 module that can be installed with:
pip install pywin32
With pywin32 you can alter the window exstyle and set the canvas to a layered window. A layered window can have a transparent colorkey and is done in the example below:
import tkinter as tk
import win32gui
import win32con
import win32api
root = tk.Tk()
root.configure(bg='yellow')
canvas = tk.Canvas(root,bg='#000000')#full black
hwnd = canvas.winfo_id()
colorkey = win32api.RGB(0,0,0) #full black in COLORREF structure
wnd_exstyle = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
new_exstyle = wnd_exstyle | win32con.WS_EX_LAYERED
win32gui.SetWindowLong(hwnd,win32con.GWL_EXSTYLE,new_exstyle)
win32gui.SetLayeredWindowAttributes(hwnd,colorkey,255,win32con.LWA_COLORKEY)
canvas.create_rectangle(50,50,100,100,fill='blue')
canvas.pack()
Explaination:
First we need the handle of the window which is called hwnd and we can get it in tkinter by .winfo_id().
Next we get the actual extended window style by GetWindowLong and ask specific for extended style information with win32con.GWL_EXSTYLE.
After that we do a bitwise operation in hexadezimal to alter the style with wnd_exstyle | win32con.WS_EX_LAYERED the result is our new_style.
Now we can set the extended style to the window with SetWindowLong. Finally we have our LayeredWindow which has additional Attributes we can work with. A transparent ColorKey can be set with SetLayeredWindowAttributes while we just use LWA_COLORKEY the alpha parameter has no use to us.
Important note: After defining a transparent colorkey, everything in that canvas with that color will be transparent.
I am trying to display an image on my desktop without any boarder or window, like an image floating in the desktop. I also want to be able to control its position once it has been created, with the arrow keys or with a line of code that changes its position with some coordinate system of some sorts. I haven't found any method after some reaserch (not in Python, at least).
If that's not possible, please recommend another programming language that can.
You can use Tkinter for this.
I made a little example:
Python3
from tkinter import Toplevel, Tk, Label, PhotoImage
win = Tk()
win.attributes('-alpha', 0.0)
win.iconify()
window = Toplevel(win)
window.geometry("500x500+100+100")
window.overrideredirect(1)
photo = PhotoImage(file="test.png")
label = Label(window, image=photo)
label.pack()
win.mainloop()
Python2
from Tkinter import Toplevel, Tk, Label
import ImageTk
win = Tk()
win.attributes('-alpha', 0.0)
win.iconify()
window = Toplevel(win)
window.geometry("500x500+100+100") # create an window 500x500 pixel, 100 pixels from the upper left corner
window.overrideredirect(1) # Take the border away
photo = ImageTk.PhotoImage(file="test.png")
label = Label(window, image=photo)
label.pack()
win.mainloop()
In Tkinter, how can I pack a canvas to the upper left corner and a button to the lower right corner? I tried with can.pack(side=...) and button.pack(side=....) but no luck. I want to get something like this Picture.
You were close. You need to incorporate one more option: anchor.
Below is a simple script to demonstrate:
import Tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(bg="red", height=100, width=100)
canvas.pack(anchor=tk.NW)
button = tk.Button(text="button")
button.pack(side=tk.RIGHT, anchor=tk.SE)
root.mainloop()
When you resize the window, notice how the canvas stays in the upper lefthand corner and the button stays in the lower righthand corner.