鱼C论坛

 找回密码
 立即注册
查看: 3977|回复: 10

[已解决]tkinter 消灭病毒问题

[复制链接]
发表于 2020-3-31 08:30:56 | 显示全部楼层 |阅读模式
10鱼币
本帖最后由 Mike_python小 于 2020-3-31 08:32 编辑

我想设置飞机如果撞到了canvas的边缘就停下来但是我这样设计时他就报错了

这是代码
  1. from tkinter import *
  2. from tkinter import messagebox
  3. from PIL import Image, ImageTk

  4. root = Tk()
  5. root.geometry('800x1000+500+0')
  6. root.title('消灭病毒,中国加油,武汉必胜!')

  7. def bind_(event):
  8.     y1= 0 + y2
  9.     x1= 0 + x2
  10.     if (y1 < 100) or (x1 < 100):
  11.         if event.keysym == 'Left':
  12.             a.move(img,-10,0)
  13.             y2 = 0 + 30
  14.             print(y1)
  15.         if event.keysym == 'Right':
  16.             a.move(img,10,0)
  17.             x2 = 0 + 30
  18.             print(x1)
  19.         if event.keysym == 'Up':
  20.             a.move(img,0,-10)
  21.             x1 = x1 + 30
  22.         if event.keysym == 'Down':
  23.             a.move(img,0,10)
  24.             x1 = x1 + 30
  25.     else:
  26.         a.move(img, 0, 0)

  27. a = Canvas(root, width=800, height=1000)
  28. a.pack()

  29. b1 = PhotoImage(file=r'C:\Users\Administrator\Desktop\飞机1.gif')
  30. img = a.create_image(420, 950, image=b1)
  31. a.bind_all('<KeyPress-Left>', bind_)
  32. a.bind_all('<KeyPress-Down>', bind_)
  33. a.bind_all('<KeyPress-Up>', bind_)
  34. a.bind_all('<KeyPress-Right>', bind_)
  35. root.mainloop()


复制代码


有哪位大佬个我指点一下怎么改?


第一张图是C:\Users\Administrator\Desktop\飞机1.gif

后两张是问题

@科技爱好者 @一个账号
最佳答案
2020-3-31 08:30:57
Mike_python小 发表于 2020-3-31 12:46
但是我这样好像也起不到我想要的效果

你是想让病毒每0.01秒移动一次?
飞机1.gif
捕获1.PNG
捕获2.PNG

最佳答案

查看完整内容

你是想让病毒每0.01秒移动一次?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-31 08:30:57 | 显示全部楼层    本楼为最佳答案   
Mike_python小 发表于 2020-3-31 12:46
但是我这样好像也起不到我想要的效果

你是想让病毒每0.01秒移动一次?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-31 11:01:06 | 显示全部楼层
你都没初始化 y2 和 x2

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
Mike_python小 + 1 + 1 感谢楼主无私奉献!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-31 12:26:42 | 显示全部楼层
一个账号 发表于 2020-3-31 11:01
你都没初始化 y2 和 x2

??? 我不是使用的类 为什么还要初始化?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-31 12:38:39 | 显示全部楼层
你这 y2 和 x2 哪里的:

    y1= 0 + y2
    x1= 0 + x2
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-31 12:39:01 | 显示全部楼层
一个账号 发表于 2020-3-31 12:38
你这 y2 和 x2 哪里的:

    y1= 0 + y2

两个变量呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-31 12:39:59 | 显示全部楼层
    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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-31 12:40:26 | 显示全部楼层

那你说一下你的 y2 和 x2 在哪里定义过
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-31 12:41:54 | 显示全部楼层
这里我想让病毒sleep0.05秒

但是我这里飞机也被sleep了  我把他放到一个函数里也不行  
你们知道这是怎么回事吗@一个账号

  1. from tkinter import *
  2. from tkinter import messagebox
  3. from PIL import Image, ImageTk
  4. import time

  5. root = Tk()
  6. root.geometry('800x1000+500+0')
  7. root.title('消灭病毒,中国加油,武汉必胜!')

  8. def bind_(event):
  9.     if event.keysym == 'Left':
  10.         a.move(img,-10,0)
  11.     if event.keysym == 'Right':
  12.         a.move(img,10,0)
  13.     if event.keysym == 'Up':
  14.         a.move(img,0,-10)
  15.     if event.keysym == 'Down':
  16.         a.move(img,0,10)
  17. x = 100
  18. y = 100

  19. a = Canvas(root, width=800, height=1000)
  20. a.pack()

  21. b1 = PhotoImage(file=r'C:\Users\Administrator\Desktop\飞机1.gif')
  22. img = a.create_image(420, 950, image=b1)

  23. b2 = PhotoImage(file=r'C:\Users\Administrator\Desktop\病毒1.gif')
  24. for i in range (1,5):
  25.     img1 = a.create_image(x, y, image=b2)


  26. img1 = a.create_image(-10, -10, image=b2)
  27. a.bind_all('<KeyPress-Left>', bind_)
  28. a.bind_all('<KeyPress-Down>', bind_)
  29. a.bind_all('<KeyPress-Up>', bind_)
  30. a.bind_all('<KeyPress-Right>', bind_)
  31. def aa():
  32.     for x in range(0,10):
  33.         a.move(img1, 0, 50)
  34.         root.update()
  35.         time.sleep(0.05)

  36. aa()

  37. root.mainloop()


复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-31 12:42:49 | 显示全部楼层
一个账号 发表于 2020-3-31 12:40
那你说一下你的 y2 和 x2 在哪里定义过

奥  你的意思是说我在前面加一个x2=0,
y2=0就行了是吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-3-31 12:46:17 | 显示全部楼层
本帖最后由 Mike_python小 于 2023-1-11 14:23 编辑
一个账号 发表于 2020-3-31 12:40
那你说一下你的 y2 和 x2 在哪里定义过


但是我这样好像也起不到我想要的效果

  1. from tkinter import *
  2. from tkinter import messagebox
  3. from PIL import Image, ImageTk

  4. root = Tk()
  5. root.geometry('800x1000+500+0')
  6. root.title('消灭病毒,中国加油,武汉必胜!')

  7. def bind_(event):
  8.     x2 = 0
  9.     y2 = 0
  10.     y1 = 0 + y2
  11.     x1= 0 + x2
  12.     if (y1 < 100) or (x1 < 100):
  13.         if event.keysym == 'Left':
  14.             a.move(img,-10,0)
  15.             y2 = 0 + 30
  16.             print(y1)
  17.         if event.keysym == 'Right':
  18.             a.move(img,10,0)
  19.             x2 = 0 + 30
  20.             print(x1)
  21.         if event.keysym == 'Up':
  22.             a.move(img,0,-10)
  23.             x1 = x1 + 30
  24.         if event.keysym == 'Down':
  25.             a.move(img,0,10)
  26.             x1 = x1 + 30
  27.     else:
  28.         a.move(img, 0, 0)

  29. a = Canvas(root, width=800, height=1000)
  30. a.pack()

  31. b1 = PhotoImage(file=r'C:\Users\Administrator\Desktop\飞机1.gif')
  32. img = a.create_image(420, 950, image=b1)
  33. a.bind_all('<KeyPress-Left>', bind_)
  34. a.bind_all('<KeyPress-Down>', bind_)
  35. a.bind_all('<KeyPress-Up>', bind_)
  36. a.bind_all('<KeyPress-Right>', bind_)
  37. root.mainloop()


复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 13:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表