我想在Pythonclass中用canvas画出一个长方形并用键盘中的左右按键让它移动,但是不动 - 爱问答

(爱问答)

我想在Pythonclass中用canvas画出一个长方形并用键盘中的左右按键让它移动,但是不动

代码入下:
from tkinter import *
class main:    
       class Rectangle:        
              def __init__(self,canvas,color):            
                    self.canvas = canvas            
                    self.id = canvas.create_rectangle(30,80,45,90,fill=color)            
                    self.x = 0            
                    self.canvas_width = self.canvas.winfo_width()            
                    self.canvas.bind_all('<KeyPress-Left>',self.turn_left)                                                                                  self.canvas.bind_all('<KeyPress-Right>',self.turn_right)        
              def draw(self):            
                    self.canvas.move(self.id,self.x,0)            
                    pos = self.canvas.coords(self.id)            
                    if pos[0] <= 0:                
                         self.x = 0            
                    elif pos[2] >= self.canvas_width:                
                         self.x = 0        
              def turn_left(self,evt):            
                    self.x = -5         
              def turn_right(self,evt):            
                    self.x = 5    
       root = Tk()    
       root.title('Exterminate Ball')    
       root.resizable(0, 0)    
       canvas = Canvas(root,width=90,height=90,bg='black',bd=0)    
       canvas.pack()    
       rectangle = Rectangle(canvas,'silver')    
       rectangle.draw()    
       root.mainloop()
(Python 3.7)

一个简单的问题,你self.x = -5  / self.x = 5    后没有重新画图,图形当然没有变化了,加两个语句就可以了

我想在Python class中用canvas画出一个长方形并用键盘中的左右按键让它移动,但是不动

下一篇:华为电脑充不上电怎么回事?

上一篇:c语言值传递问题

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?