1

Столкнулся с такой проблемой: мне нужно получить в tkinter прозрачный фон. Если ставлю прозрачность фона на максимум командой root.attributes("-alpha", 0), то, логично, пропадают все элементы типа Label и т.д. Мне нужно, чтобы при запуске программы на экране просто появлялся текст. Нашел в интернете и адаптировал похожую программу:

import tkinter as tk
root = tk.Tk()
#-----------------------------------CONFIG-------------------
root.overrideredirect(True)
root.lift()
root.wm_attributes("-disabled", True)
root.wm_attributes("-transparentcolor", "white")
root.geometry("+10+670")
#-----------------------------------Labels-------------------
text1 = "Text1: " + "this is tryal command. It was used only to understand 
style of a window."
text2 = "Text2: " + "this is tryal command. It was used only to understand 
style of a window. White space above"
lbl1 = tk.Label(root, text=text1, font='Arial 15', fg="Red", bg='gray')
lbl2 = tk.Label(root, text=text2, font='Arial 15', fg="Red", bg='gray')
lbl3 = tk.Label(root, text='sdfghjk', font='Arial 15', fg="Red", bg='white')
lbl1.pack()
lbl2.pack()
#----------------------------------------------------------
lbl1.mainloop()

Однако, здесь я не могу разбить текст на две группы, чтобы, к примеру, одна часть была внизу экрана, а другая - слева. Также имеется и другой недостаток: если один текст больше другого (как в примере), то рядом с меньшим текстом появляется белая область. Умные люди, исправьте, пожалуйста, а то я весь интернет облазил - похожего не нашел, там лишь фотографии вставляют, а не тексты... или, может, кто-нибудь знает библиотеку, с помощью которой можно это осуществить.

1 ответ 1

2

Сам измучился пытаясь это сделать.Я уж думал что на моем macos этого не как не сделать но нашел вариант.

Под Windows:

import Tkinter as tk # Python 2
import tkinter as tk # Python 3
root = tk.Tk()
label = tk.Label(root, image='text', bg='white',font=["Comic Sans MS",30])
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()

Сам на маке — не пробовал

Следующий вариант


должен работать под mac / linux(или ubuntu и т.д....) (т.е unix-подобные ОС).
Но на моем маке это не сработало:

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")
label = tk.Label(root, text="hello")
# Set the label background color to a transparent color
label.config(bg='systemTransparent')
label.pack()

root.mainloop()

Но на моем маке это не сработало

И наконец мой третий вариант который сработал у меня на маке:

from tkinter import*
import tkinter.ttk as ttk
w=Tk()
w2 = Toplevel(w)
w2.geometry("550x400")
w2.geometry("+300+300")
w2.wm_overrideredirect(True)  # Removes the titlrbar and borders
w2.update()   # Don't need if not on macos
w2.lift()
w2.attributes('-alpha', 1) # Makes the window transparent
w2.attributes('-topmost', 1)
w2.wm_attributes('-transparent', True)
w2.config(bg='systemTransparent')
w.attributes("-alpha",0)
w.iconify()
l=Label(w2,text="✅Congratulation! Programm is starting...",font=["Times",30],fg="green")
l.pack()
l=Label(w2)
l.pack()
pbar=ttk.Progressbar(w2)
pbar.pack()
pbar.start()
b=ttk.Button(w2,text="quit",command=quit)
b.pack()
w.mainloop()

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.