会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132370个问题

1.png

老师,问题在图片麻烦帮忙解答一下

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2103楼

一、代码如下:

"""测试tkinter的label组件的基本用法,使用面向对象的方法"""

from tkinter import *

class Application(Frame):
    def __init__(self,master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.createWidget()

    def createWidget(self):
        """创建组件"""
        self.btn01 = Button(self,text="登录",width=6,height=3,anchor=E,command=self.login)
        self.btn01.pack()
        self.label01 = Label(self,text="第一个标签",width=10,height=2,bg="black",fg="white")
        self.label01.pack()
        self.label02 = Label(self,text="csh",width=10,height=2,bg="blue",fg="white",font=("黑体",30))
        self.label02.pack()

        # 显示图像
        global photo   #定义全局变量photo,若是局部变量,本方法执行完毕后,图像对象会销毁而不显示
        photo = PhotoImage(file="d:/PythonProject/Examplegif.gif")
        self.label03 = Label(self,image=photo)
        self.label03.pack()

        self.label04 = Label(self,text="河北省\n黄骅港\n崔树辉",justify="left",width=30,height=10,bg="red",fg="yellow",font=("微软雅黑",10))
        self.label04.pack()


if __name__ == "__main__":
    root = Tk()
    root.geometry("600x400+200+300")
    app = Application(master=root)
    app.mainloop()


二、报错信息:

Traceback (most recent call last):

  File "D:/MyPythonProject/venv/a/module_A2.py", line 34, in <module>

    app = Application(master=root)

  File "D:/MyPythonProject/venv/a/module_A2.py", line 10, in __init__

    self.createWidget()

  File "D:/MyPythonProject/venv/a/module_A2.py", line 14, in createWidget

    self.btn01 = Button(self,text="登录",width=6,height=3,anchor=E,command=self.login)

AttributeError: 'Application' object has no attribute 'login'


三、我刚才看了前面有同学提问了这个问题,当时老师解答是缩进原因,我检查了这段代码,没有发现缩进问题,请老师费心解答,谢谢。

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2104楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2105楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2106楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2107楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2108楼

#coding=utf-8
'''
新增功能
    左上角文字绘制
    左上角输出坦克数量
'''
import pygame

SCREEN_WIDTH = 700
SCREEN_HEIGHT = 500
BG_COLOR = pygame.Color(0, 0, 0)
TEXT_COLOR = (255, 0, 0)
class MainGame():
    window = None
    def __init__(self):
        pass

    # 开始游戏
    def startGame(self):
        #加载窗口
        #初始化窗口
        pygame.display.init()
        #设置窗口大小及显示
        MainGame.window = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
        pygame.display.set_caption('坦克大战1.03')

        while True:
            MainGame.window.fill(BG_COLOR)
            #获取事件
            self.getEvent()
            #绘制文字
            MainGame.window.blit(self.getTextSurface('敌方坦克剩余数量%d' % 6), (10, 10))
            pygame.display.update()

    # 结束游戏
    def endGame(self):
        print('谢谢使用')
        exit()
    #左上角文字绘制
    def getTextSurface(self, text):
        #初始化字体模块
        pygame.font.init()
        #查看所有字体
        print(pygame.font.get_fonts())
        #获取字体对象
        font = pygame.font.SysFont('kaiti',18)
        #绘制字体
        textSurface = font.render(text, True, TEXT_COLOR)
    def getEvent(self):
        #获取所有事件
        eventList = pygame.event.get()
        #遍历事件
        for event in eventList:
            #判断键盘按下的是关闭还是键盘按下
            #如果是关闭退出
            if event.type == pygame.QUIT:
                self.endGame()
            #如果是按下键
            if event.type == pygame.KEYDOWN:
                #判断上下左右
                if event.key == pygame.K_LEFT:
                    print("按下左键向左运动")
                elif event.key == pygame.K_RIGHT:
                    print("按下右键向右运动")
                elif event.key == pygame.K_UP:
                    print("按下上键向上运动")
                elif event.key == pygame.K_DOWN:
                    print("按下下键向下运动")
class Tank():
    def __init__(self):
        pass

    # 移动
    def move(self):
        pass

    # 射击
    def shot(self):
        pass

    # 展示坦克的方法
    def displayTank(self):
        pass
class MyTank(Tank):
    def __init__(self):
        pass
class EnemyTank(Tank):
    def __init__(self):
        pass
class Bullet():
    def __init__(self):
        pass

    # 移动
    def move(self):
        pass

    # 展示子弹的方法
    def displayBullet(self):
        pass
class Wall():
    def __init__(self):
        pass

    # 展示墙壁的方法
    def displayWall(self):
        pass
class Explode():
    def __init__(self):
        pass

    # 展示爆炸效果的方法
    def displayExplode(self):
        pass
class Music():
    def __init__(self):
        pass

    # 播放音乐
    def play(self):
        pass

if __name__ == '__main__':
    MainGame().startGame()
    #MainGame().getTextSurface()

老师我的window为什么一直是none

image.png

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

1.png

  1. 这个是不是因为相对路径的问题

  2. 如果是应该怎么改,如果不是是哪里的问题

Python 全系列/第二阶段:Python 深入与提高/模块 2111楼
Python 全系列/第二阶段:Python 深入与提高/模块 2112楼
Python 全系列/第二阶段:Python 深入与提高/异常机制 2114楼
Python 全系列/第二阶段:Python 深入与提高/异常机制 2115楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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