tkinter 消灭病毒问题
本帖最后由 Mike_python小 于 2020-3-31 08:32 编辑我想设置飞机如果撞到了canvas的边缘就停下来但是我这样设计时他就报错了
这是代码
from tkinter import *
from tkinter import messagebox
from PIL import Image, ImageTk
root = Tk()
root.geometry('800x1000+500+0')
root.title('消灭病毒,中国加油,武汉必胜!')
def bind_(event):
y1= 0 + y2
x1= 0 + x2
if (y1 < 100) or (x1 < 100):
if event.keysym == 'Left':
a.move(img,-10,0)
y2 = 0 + 30
print(y1)
if event.keysym == 'Right':
a.move(img,10,0)
x2 = 0 + 30
print(x1)
if event.keysym == 'Up':
a.move(img,0,-10)
x1 = x1 + 30
if event.keysym == 'Down':
a.move(img,0,10)
x1 = x1 + 30
else:
a.move(img, 0, 0)
a = Canvas(root, width=800, height=1000)
a.pack()
b1 = PhotoImage(file=r'C:\Users\Administrator\Desktop\飞机1.gif')
img = a.create_image(420, 950, image=b1)
a.bind_all('<KeyPress-Left>', bind_)
a.bind_all('<KeyPress-Down>', bind_)
a.bind_all('<KeyPress-Up>', bind_)
a.bind_all('<KeyPress-Right>', bind_)
root.mainloop()
有哪位大佬个我指点一下怎么改?{:10_254:}
第一张图是C:\Users\Administrator\Desktop\飞机1.gif
后两张是问题
@科技爱好者 @一个账号 Mike_python小 发表于 2020-3-31 12:46
但是我这样好像也起不到我想要的效果
你是想让病毒每0.01秒移动一次? 你都没初始化 y2 和 x2 一个账号 发表于 2020-3-31 11:01
你都没初始化 y2 和 x2
??? 我不是使用的类 为什么还要初始化? 你这 y2 和 x2 哪里的:
y1= 0 + y2
x1= 0 + x2 一个账号 发表于 2020-3-31 12:38
你这 y2 和 x2 哪里的:
y1= 0 + y2
两个变量呀 if (y1 < 100) or (x1 < 100):
if event.keysym == 'Left':
a.move(img,-10,0)
y2 = 0 + 30
print(y1)
if event.keysym == 'Right':
a.move(img,10,0)
x2 = 0 + 30
print(x1)
if event.keysym == 'Up':
a.move(img,0,-10)
x1 = x1 + 30
if event.keysym == 'Down':
a.move(img,0,10)
x1 = x1 + 30 Mike_python小 发表于 2020-3-31 12:39
两个变量呀
那你说一下你的 y2 和 x2 在哪里定义过 这里我想让病毒sleep0.05秒
但是我这里飞机也被sleep了我把他放到一个函数里也不行
你们知道这是怎么回事吗@一个账号
from tkinter import *
from tkinter import messagebox
from PIL import Image, ImageTk
import time
root = Tk()
root.geometry('800x1000+500+0')
root.title('消灭病毒,中国加油,武汉必胜!')
def bind_(event):
if event.keysym == 'Left':
a.move(img,-10,0)
if event.keysym == 'Right':
a.move(img,10,0)
if event.keysym == 'Up':
a.move(img,0,-10)
if event.keysym == 'Down':
a.move(img,0,10)
x = 100
y = 100
a = Canvas(root, width=800, height=1000)
a.pack()
b1 = PhotoImage(file=r'C:\Users\Administrator\Desktop\飞机1.gif')
img = a.create_image(420, 950, image=b1)
b2 = PhotoImage(file=r'C:\Users\Administrator\Desktop\病毒1.gif')
for i in range (1,5):
img1 = a.create_image(x, y, image=b2)
img1 = a.create_image(-10, -10, image=b2)
a.bind_all('<KeyPress-Left>', bind_)
a.bind_all('<KeyPress-Down>', bind_)
a.bind_all('<KeyPress-Up>', bind_)
a.bind_all('<KeyPress-Right>', bind_)
def aa():
for x in range(0,10):
a.move(img1, 0, 50)
root.update()
time.sleep(0.05)
aa()
root.mainloop()
一个账号 发表于 2020-3-31 12:40
那你说一下你的 y2 和 x2 在哪里定义过
奥你的意思是说我在前面加一个x2=0,
y2=0就行了是吗 本帖最后由 Mike_python小 于 2023-1-11 14:23 编辑
一个账号 发表于 2020-3-31 12:40
那你说一下你的 y2 和 x2 在哪里定义过
但是我这样好像也起不到我想要的效果
from tkinter import *
from tkinter import messagebox
from PIL import Image, ImageTk
root = Tk()
root.geometry('800x1000+500+0')
root.title('消灭病毒,中国加油,武汉必胜!')
def bind_(event):
x2 = 0
y2 = 0
y1 = 0 + y2
x1= 0 + x2
if (y1 < 100) or (x1 < 100):
if event.keysym == 'Left':
a.move(img,-10,0)
y2 = 0 + 30
print(y1)
if event.keysym == 'Right':
a.move(img,10,0)
x2 = 0 + 30
print(x1)
if event.keysym == 'Up':
a.move(img,0,-10)
x1 = x1 + 30
if event.keysym == 'Down':
a.move(img,0,10)
x1 = x1 + 30
else:
a.move(img, 0, 0)
a = Canvas(root, width=800, height=1000)
a.pack()
b1 = PhotoImage(file=r'C:\Users\Administrator\Desktop\飞机1.gif')
img = a.create_image(420, 950, image=b1)
a.bind_all('<KeyPress-Left>', bind_)
a.bind_all('<KeyPress-Down>', bind_)
a.bind_all('<KeyPress-Up>', bind_)
a.bind_all('<KeyPress-Right>', bind_)
root.mainloop()
页:
[1]