简体   繁体   English

Tkinter 我想添加背景图像并希望在其上显示小部件,但小部件隐藏在图像后面

[英]Tkinter i want to add a background image and want to display widgets on top of that , but widgets hiding behind the image

the arc is hiding behind the image and i want to display it above the image and how to add more images which overlay on he background image弧隐藏在图像后面,我想将其显示在图像上方以及如何添加更多覆盖在背景图像上的图像

from itertools import cycle
from random import randrange
from tkinter import Tk , Canvas , messagebox , font
from tkinter import *
from tkinter.ttk import *

canvasw = 1200
canvash = 800

win = Tk()

c = Canvas(win, bg="blue", height=canvash, width=canvasw)
backg = PhotoImage(file = "back.png")
background_label = Label(win, image=backg)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

eg = PhotoImage(file = "eg.png")
ep = PhotoImage(file = "ep.png")
er = PhotoImage(file = "er.png")
ey = PhotoImage(file = "ey.png")


ew = 45
eh = 55
es = 10

ev = 500
ei = 4000
dfcl = 0.95
catchcol = 'black'
catw = 100
cath = 100
catsx1 = canvasw / 2 - catw /2
catsy1 = canvash - cath -20
catsx2 = catsx1 + catw 
catsy2 = catsy1 + cath
cat = c.create_arc(catsx1,catsy1,catsx2,catsy2, start= 200 ,extent = 140, style='arc', outline=catchcol, width=3) 

c.pack()
win.mainloop()

the arc is hiding behind the image and i want to display it above the image弧隐藏在图像后面,我想将其显示在图像上方

relwidth=1, relheight=1 is causing the the arc not be shown relwidth=1, relheight=1导致弧线不显示

so do也一样

background_label.place(x=0, y=0)

relwidth=1, relheight=1 sets the label to be as big as the window, so the label will cover everything. relwidth=1, relheight=1将 label 设置为与 window 一样大,因此 label 将涵盖所有内容。

You should use create_image() to put the background image into canvas instead of using label:您应该使用create_image()将背景图像放入 canvas 而不是使用 label:

c.create_image(0, 0, image=backg, anchor='nw')

Note that you should avoid importing modules as below:请注意,您应该避免导入以下模块:

from tkinter import Tk , Canvas , messagebox , font
from tkinter import *
from tkinter.ttk import *

The first import is override by the second import, so the first import is not necessary.第一个导入被第二个导入覆盖,因此第一个导入不是必需的。 Also some of the widgets in tkinter will be override by the third import, ie widgets from ttk .此外, tkinter中的一些小部件将被第三次导入覆盖,即来自ttk的小部件。

You should import the modules as below:您应该按如下方式导入模块:

import tkinter as tk
from tkinter import ttk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM