如何解决有什么办法可以加载图像并将其放在Tkinter中的窗口背景上?
这是我的代码,我上一个问题有相同的代码。我想在窗口上载图片。现在,它仅显示基本的天空,基本的太阳和基本的草。我试图找到一个可以帮助我的模块。我找到了枕头,但我不知道如何使用它。你能帮我吗?或者我要使用的模块无法与此窗口配合使用,或者枕头不能用于此目的。
#import modules
from itertools import cycle
from random import randrange
from tkinter import Canvas,Tk,messageBox,font
#assigning the window variables
canvas_width = 800
canvas_height = 400
#drawing the sky,sun,and grass and making the window
root = Tk ()
c = Canvas(root,width=canvas_width,height=canvas_height,background='deep sky blue')
c.create_rectangle(-5,canvas_height - 100,canvas_width + 5,canvas_height + 5,\
fill='sea green',width=0)
c.create_oval(-80,-80,120,fill='orange',width=0)
c.pack()
color_cycle = cycle(['light blue','light green','light pink','light yellow','light cyan' ])
#assigning important variables
egg_width = 45
egg_height = 55
egg_score = 10
egg_speed = 200
egg_interval = 3000
difficulty_factor = 0.95
catcher_color ='blue'
catcher_width = 100
catcher_height = 100
catcher_start_x = canvas_width / 2 - catcher_width / 2
catcher_start_y = canvas_height - catcher_height - 20
catcher_start_x2 = catcher_start_x + catcher_width
catcher_start_y2 = catcher_start_y + catcher_height
#creating the arc for the catcher to catch the eggs
catcher = c.create_arc(catcher_start_x,catcher_start_y,\
catcher_start_x2,catcher_start_y2,start=200,extent=140,\
style='arc',outline=catcher_color,width=3)
#Writing the score and how much lives you have
game_font = font.nametofont('TkFixedFont')
game_font.config(size = 18)
score = 0
score_text = c.create_text(10,10,anchor = 'nw',font = game_font,fill = 'DarkBlue',\
text = 'score: ' + str(score))
lives_remaining = 3
lives_text = c.create_text(canvas_width - 10,anchor='ne',font=game_font,fill = ('darkblue'),\
text='Lives: ' + str(lives_remaining))
eggs = []
#creating the egg
def create_egg():
x = randrange(10,740)
y = 40
new_egg = c.create_oval(x,y,x + egg_width,y + egg_height,fill=next(color_cycle),width=0)
eggs.append(new_egg)
root.after(egg_interval,create_egg)
#moving the eggs
def move_eggs():
for egg in eggs:
(egg_x,egg_y,egg_x2,egg_y2) = c.coords(egg)
c.move(egg,10)
if egg_y2 > canvas_height:
egg_dropped(egg)
root.after(egg_speed,move_eggs)
#if the eggs reach the bottom of the window,minus one life and delete the egg
def egg_dropped(egg):
eggs.remove(egg)
c.delete(egg)
lose_a_life()
#if you have 0 lives,write Game Over!
if lives_remaining == 0:
messageBox.showinfo('Game Over!','Final score: ' \
+ str(score))
root.destroy()
#lose a life
def lose_a_life():
global lives_remaining
lives_remaining -= 1
c.itemconfigure(lives_text,text = 'lives: ' + str(lives_remaining))
#If the catcher(the arc) catches an egg,add 10 to the score and delete it
def check_catch():
(catcher_x,catcher_y,catcher_x2,catcher_y2) = c.coords(catcher)
for egg in eggs:
(egg_x,egg_y2) = c.coords(egg)
if catcher_x < egg_x and egg_x2 < catcher_x2 and catcher_y2 - egg_y2 < 40:
eggs.remove(egg)
c.delete(egg)
increase_score(egg_score)
root.after(99,check_catch)
#increase the score
def increase_score(points):
global score,egg_speed,egg_interval
score += points
egg_speed = int(egg_speed * difficulty_factor)
egg_interval = int(egg_interval * difficulty_factor)
c.itemconfigure(score_text,text='score: ' + str(score))
#move left
def move_left(event):
(x1,y1,x2,y2) = c.coords(catcher)
if x1 > 0:
c.move(catcher,-20,0)
#move right
def move_right(event):
(x1,y2) = c.coords(catcher)
if x2 < canvas_width:
c.move(catcher,20,0)
#If the left or right arrow key is hit,move that direction
c.bind('<Left>',move_left)
c.bind('<Right>',move_right)
c.focus_set()
#Call the game functions
root.after(1000,create_egg)
root.after(1000,move_eggs)
root.after(1000,check_catch)
#Call the main Tkinter loop
root.mainloop()
解决方法
尝试这个:
from tkinter import *
from PIL import ImageTk,Image # PIL will import pillow
root = Tk()
my_img = ImageTk.PhotoImage(Image.open("C:\\path\\picture.jpg"))
my_label = Label(image=my_img)
my_label.pack()
root.mainloop()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。