我的代码创建一个带有背景和按钮的窗口。当您按下按钮时,游戏开始。但是,该按钮看起来很奇怪,因为它导致一条灰线覆盖了背景的顶部。如何创建一个不会引起灰线,而是在背景上方显示按钮的按钮:
from tkinter import*
import turtle
import random
master = Tk()
def code1():
turtle.bgcolor('orange')
h = [0]
a = [0]
b = [0]
fcoord = [0,0,0]
pos = []
def home(x,y):
x = 0
y = 0
a[0] = 0
b[0] = 0
h[0] = 0
fcoord[2] = 0
pos[:] = []
turtle.hideturtle()
turtle.clear()
turtle.pu()
turtle.color("white")
turtle.goto(0,0)
turtle.write("Play")
turtle.write('')
turtle.title("Snake")
turtle.onscreenclick(start)
turtle.mainloop()
def level_1():
turtle.clear()
turtle.pu()
turtle.speed(0)
turtle.pensize(20)
turtle.color("green")
turtle.goto(-220,220)
turtle.pd()
turtle.goto(220,220)
turtle.goto(220,-220)
turtle.goto(-220,-220)
turtle.goto(-220,220)
turtle.pu()
turtle.goto(0,0)
def start(x,y):
turtle.onscreenclick(None)
level_1()
tfood = turtle.Turtle()
tfood.hideturtle()
tfood.pu()
tfood.speed(0)
tfood.shape("circle")
tfood.color("white")
tscore = turtle.Turtle()
tscore.hideturtle()
tscore.pu()
tscore.speed(0)
tscore.goto(100,-250)
tscore.write("Eats :" + str(a[0]), align="center",font=(10))
while x > -210 and x < 210 and y > -210 and y <210:
if fcoord[2] == 0:
food(tfood)
fcoord[2] = 1
turtle.onkey(u,"Up")
turtle.onkey(l,"Left")
turtle.onkey(r,"Right")
turtle.onkey(d,"Down")
turtle.listen()
move()
x = turtle.xcor()
y = turtle.ycor()
if x > fcoord[0]*20-5 and x < fcoord[0]*20+5 and y > fcoord[1]*20-5 and y < fcoord[1]*20+5:
fcoord[2] = 0
tfood.clear()
a[0] += 1
tscore.clear()
tscore.write("Eats :" + str(a[0]), align="center",font=(10))
if len(pos) > 1:
for i in range(1,len(pos)):
if x < pos[i][0]+5 and x > pos[i][0]-5 and y < pos[i][1]+5 and y > pos[i][1]-5:
tscore.clear()
tfood.clear()
gameover()
tscore.clear()
tfood.clear()
gameover()
def food(tfood):
x = random.randrange(-8,8,1)
y = random.randrange(-8,8,1)
fcoord[0] = x
fcoord[1] = y
tfood.hideturtle()
tfood.pu()
tfood.shape("circle")
tfood.color("blue")
tfood.goto(x*20,y*20)
tfood.stamp()
def u():
if h[0] == 270:
pass
else:
h[0] = 90
def d():
if h[0] == 90:
pass
else:
h[0] = 270
def l():
if h[0] == 0:
pass
else:
h[0] = 180
def r():
if h[0] == 180:
pass
else:
h[0] = 0
def move():
turtle.pensize(10)
turtle.color("yellow")
turtle.pu()
turtle.speed(3)
turtle.setheading(h[0])
turtle.shape("circle")
turtle.stamp()
turtle.fd(20)
x = turtle.xcor()
y = turtle.ycor()
if b[0] > a[0]:
turtle.clearstamps(1)
pos.insert(0,[round(x),round(y)])
pos.pop(-1)
else:
pos.insert(0,[round(x),round(y)])
b[0] += 1
def gameover():
turtle.onscreenclick(home)
turtle.speed(0)
turtle.pu()
turtle.goto(0,150)
turtle.color("red")
turtle.write("Game Over",align="center", font=(10))
turtle.goto(0,50)
turtle.write("Score:" + str(a[0]),align="center",font=(10))
turtle.goto(200,-200)
turtle.write("(Click anywhere to return to the main menu)",align="right",font=(0.0000001))
turtle.onscreenclick(home)
turtle.mainloop()
if __name__ == '__main__':
home(0,0)
button = Button(master, text="Run the code", command=code1)
button.pack()
photo = PhotoImage(file = "bg.gif")
w = Label(master, image=photo)
w.pack()
ent = Entry(master)
ent.pack()
ent.focus_set()
mainloop()
答案 0 :(得分:0)
他正在使用导入ImageTk。我没有模块名称ImageTk。
那不是answer to which I linked的相关部分。您可以继续使用tkinter的PhotoImage()
并进行以下操作:
from tkinter import *
master = Tk()
canvas = Canvas(master, width=250, height=250)
canvas.pack()
photo = PhotoImage(file="bg.gif")
canvas.create_image(125, 125, image=photo)
quit_button = Button(master, text="Quit", command=master.quit, anchor='w')
quit_button_window = canvas.create_window(15, 15, anchor='nw', window=quit_button)
master.mainloop()
我相信create_window()
是您寻求的魔法。