会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134310个问题
Python全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 677楼

"""开发画图软件的菜单 """
#coding = "utf-8"

from tkinter import *
from tkinter.filedialog import *  # 引用对话框
from tkinter.colorchooser import *  # 引用颜色选择器
# 窗口的宽度和高度
win_width = 900
win_height = 450

class Application(Frame):
    def __init__ (self,master=None,bgcolor="#000000"):
        super().__init__(master)
        self.master=master
        self.bgcolor=bgcolor
        self.x= 0
        self.y= 0
        self.fgcolor="#ff0000"
        self.lastDraw = 0    #表示最后绘制的图形id
        self.startDrawFlag = False
        self.pack()
        self.createWidget()

    def createWidget(self):
        # 创建绘图区
        drawpad =Canvas(root,width=win_width,height=win_height*0.9,bg=self.bgcolor)
        drawpad.pack()

        # 创建按钮
        btn_srart = Button(root,text="开始",name="start")
        btn_srart.pack(side="left",padx="10")
        btn_pen = Button(root,text="画笔",name="pen")
        btn_pen.pack(side="left",padx="10")
        btn_rect = Button(root,text="矩形",name="rect")
        btn_rect.pack(side="left",padx="10")
        btn_clear = Button(root,text="清屏",name="clear")
        btn_clear.pack(side="left",padx="10")
        btn_erasor = Button(root,text="橡皮擦",name="erasor")
        btn_erasor.pack(side="left",padx="10")
        btn_line = Button(root,text="直线",name="line")
        btn_line.pack(side="left",padx="10")
        btn_lineArrow = Button(root,text="箭头直线",name="lineArrow")
        btn_lineArrow.pack(side="left",padx="10")
        btn_color = Button(root,text="颜色",name="color")
        btn_color.pack(side="left",padx="10")

        # 事件处理
        btn_pen.bind_class("Button","<1>",self.eventManager)

    def eventManager(self, event):
        name = event.widget.winfo_name()
        print(name)
        if name == "line":
            self.drawpadbind("<B1-Motion>",self.myline)

    def myline(self,event):
        self.drawpad.create_line(self.x,self.y,fill=self.fgcolor)



if __name__ == '__main__':
    root = Tk()
    root.geometry(str(win_width)+"x"+str(win_height)+"+200+300")
    app = Application(master=root)
    root.title("画图软件")
    root.mainloop()
Exception in Tkinter callback
Traceback (most recent call last):
  File "D:\software\Anaconda\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\admin\Desktop\code\深入与提高\画图软件开发1.py", line 54, in eventManager
    self.drawpadbind("<B1-Motion>",self.myline)
AttributeError: 'Application' object has no attribute 'drawpadbind'
line

这是什么原因导致的?

Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 678楼
Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 679楼
Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 680楼
Python全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 681楼
Python全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 684楼
Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 685楼
Python全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 686楼
Python全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 688楼

老师我想问一下我按下空格还是会停止移动,我代码和你代码一样的
pygametime
SCREEN_WIDTH=SCREEN_HEIGHT=BG_COLOR=pygame.Color()  text_COLOR=pygame.Color()
MainGame():
    window=my_tank=():
        ():
         pygame.display.init()
        MainGame.window=pygame.display.set_mode([SCREEN_WIDTHSCREEN_HEIGHT])
         MainGame.my_tank=Tank()
        pygame.display.set_caption()
        :
            time.sleep()
            MainGame.window.fill(BG_COLOR)
            .getEvent()
            MainGame.window.blit(.getTextSuface(%)())
            MainGame.my_tank.displayTank()
            MainGame.my_tank.stop:
             MainGame.my_tank.move()
            pygame.display.update()


    ():
        ()
        ()
    (text):
        pygame.font.init()
        font=pygame.font.SysFont()
        textSuface=font.render(texttext_COLOR)
        textSuface
    ():
        eventList=pygame.event.get()
        event eventList:
            event.type == pygame.QUIT:
                .endGame()
            event.type == pygame.KEYDOWN:

                event.key == pygame.K_LEFT:
                    MainGame.my_tank.direction = MainGame.my_tank.stop=()
                event.key == pygame.K_RIGHT:
                    MainGame.my_tank.direction = MainGame.my_tank.stop = ()
                event.key == pygame.K_UP:
                    MainGame.my_tank.direction = MainGame.my_tank.stop = ()
                event.key == pygame.K_DOWN:
                    MainGame.my_tank.direction = MainGame.my_tank.stop = ()
                event.key==pygame.K_SPACE:
                    ()

                    event.type == pygame.KEYUP:
                event.key==pygame.K_UP pygame.K_DOWN pygame.K_RIGHT pygame.K_RIGHT:
                    MainGame.my_tank.stop = Tank():
    (lefttop):
        .images = {: pygame.image.load(): pygame.image.load(): pygame.image.load(): pygame.image.load()
                       }
        .direction=.image=.images[.direction]
        .rect=.image.get_rect()
        .rect.left=left
        .rect.top=top
        .speed=.stop=():
        ():
        .direction == :
            .rect.left > :
               .rect.left -= .speed
        .direction == :
            .rect.right < :
               .rect.right += .speed
        .direction == :
            .rect.top+.rect.height < :
               .rect.top += .speed
        .direction == :
            .rect.top>:
               .rect.top -= .speed
    ():
        .image=.images[.direction]
        MainGame.window.blit(.image.rect)
Mytank(Tank):
    ():
        Enemytank(Tank):
    ():
        Bullet():
    ():
        ():
        ():
        Wall():
    ():
        ():
        Music():
    ():
        ():
        Explode():
    ():
        ():
        __name__==:
    MainGame().startGame()


Python全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 690楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637