Is there a way to create a "Loading Screen" in Python 3.x using Tkinter? I mean like the loading screen for Adobe Photoshop, with transparency and so on. I managed to get rid of the frame border already using:
root.overrideredirect(1)
But if I do this:
root.image = PhotoImage(file=pyloc+'\startup.gif')
label = Label(image=root.image)
label.pack()
the image displays fine, but with the grey window background instead of transparency.
Is there a way of adding transparency to a window, but still displaying the image correctly?
It is possible, but it's OS-dependent. This will work in Windows:
import Tkinter as tk # Python 2
import tkinter as tk # Python 3
root = tk.Tk()
# The image must be stored to Tk or it will be garbage collected.
root.image = tk.PhotoImage(file='startup.gif')
label = tk.Label(root, image=root.image, bg='white')
root.overrideredirect(True)
root.geometry("+250+250")
root.lift()
root.wm_attributes("-topmost", True)
root.wm_attributes("-disabled", True)
root.wm_attributes("-transparentcolor", "white")
label.pack()
label.mainloop()
Here is a solution for macOS :
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="photoshop-icon.gif")
# 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()
(tested on macOS Sierra 10.12.21)
没有跨平台的方法可以在 tkinter 中使背景透明。
Just use root.config(bg='')
, that's all, for example:
from tkinter import *
root = Tk()
root.configure(bg='')
It's simple: use root.attributes()
In your case, it'd be something like root.attributes("-alpha", 0.5)
where 0.5 is the transparency you want, 0 being fully transparent to 1 being opaque.
你可以这样做: window.attributes("-transparentcolor", "somecolor")
For just doing a single image you can do the following.
label = Label(root)
label.config(image='image.gif')
label.config(bg='systemTransparent')
this appears to allow the gif and the alpha channel to shine, on macOS specifically.
pqiv
The "Linux Way" seems to be installing another package:
$ sudo apt install pqiv
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
pqiv
0 upgraded, 1 newly installed, 0 to remove and 11 not upgraded.
Need to get 34.6 kB of archives.
After this operation, 136 kB of additional disk space will be used.
Get:1 http://ca.archive.ubuntu.com/ubuntu xenial/universe amd64 pqiv amd64 2.2-1 [34.6 kB]
Fetched 34.6 kB in 0s (96.0 kB/s)
Selecting previously unselected package pqiv.
(Reading database ... 442035 files and directories currently installed.)
Preparing to unpack .../archives/pqiv_2.2-1_amd64.deb ...
Unpacking pqiv (2.2-1) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for mime-support (3.59ubuntu1) ...
Setting up pqiv (2.2-1) ...
But as I've had to install xdotool
and other packages for my application what's another one right? Plus it will make the docker folks happy :)
The good news is it is only 136KB and automatically places the splash screen in the center of the active monitor instead of the center of the primary monitor or the center of the X11 screen (which can look funny on three monitor systems of different resolutions).
pqiv
From the command line (which you can easily duplicate inside Python with os.popen()
or subprocess.Popen()
) you simply type:
pqiv -c -c -i m.png
Here's what it looks like with my png
image:
pqiv
In the terminal I have to send Control + C when loading is finished. In Python you would have to grep
the output from ps
and kill
the job. Pretty straight forward in Linux I guess but probably foreign to our Windows friends.
Credit to Super User answer .
A cross-platform solution:
from sys import platform
import tkinter as tk
root = tk.Tk()
if platform == "linux" or platform == "linux2":
root.overrideredirect(True)
root.wait_visibility(root)
root.wm_attributes("-alpha", 0.5)
elif platform == "darwin":
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="photoshop-icon.gif")
# 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()
elif platform == "win32":
root.image = tk.PhotoImage(file='startup.gif')
label = tk.Label(root, image=root.image, bg='white')
root.overrideredirect(True)
root.geometry("+250+250")
root.lift()
root.wm_attributes("-topmost", True)
root.wm_attributes("-disabled", True)
root.wm_attributes("-transparentcolor", "white")
label.pack()
root.mainloop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.