7

フレームとしてラベルが付いたウィンドウがあります。背景に画像が欲しかったのでこれにしました。しかし今、私が使用している他のラベルに問題があります。私が実際にラベル付けするために使用した他のラベルには、透明な背景がありません。これらのラベルの背景を透明にする方法はありますか?

import Tkinter as tk

root = tk.Tk()
root.title('background image')

image1 = Tk.PhotoImage(file='image_name.gif')

# get the image size
w = image1.width()
h = image1.height()

# make the root window the size of the image
root.geometry("%dx%d" % (w, h))

# root has no image argument, so use a label as a panel
panel1 = tk.Label(root, image=image1)
panel1.pack(side='top', fill='both', expand='yes')

# put a button/label on the image panel to test it
label1 = tk.Label(panel1, text='here i am')
label1.pack(side=Top)

button2 = tk.Button(panel1, text='button2')
button2.pack(side='top')

# start the event loop
root.mainloop()
4

4 に答える 4

9

私はそれが役立つと思います、すべての黒は透明になります

root.wm_attributes('-transparentcolor','black')
于 2016-09-01T12:19:32.430 に答える
8

Tk の透明な背景ではサポートされていません。

于 2012-05-05T10:59:22.343 に答える
2

これを使って :

from tkinter import *

main=Tk()
photo=PhotoImage(file='test.png')
Label(main,image=photo,bg='grey').pack()
#your other label or button or ...
main.wm_attributes("-transparentcolor", 'grey')
main.mainloop()

この作業を使用するbg='grey'場合は、7行目で変更できます

幸運を :)

于 2020-03-23T20:59:45.197 に答える