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

一、代码如下:

tkinter *

(Frame):
    (,master=):
        ().(master)
        .master = master
        .pack()
        .createWidget()

    ():
        Entry().grid(=,=,=,=,=)

        textbtn = ((,,,),
                   (,,,),
                   (,,,),
                   (,,,),
                   (,,,),
                   (,))
        rindex,r (textbtn):
            cindex,c (r):
                c == :
                    Button(,=c,=)\
                        .grid(=rindex+,=cindex,=,=EW)
                c == :
                    Button(,=c,=)\
                        .grid(=rindex+,=cindex,=,=NSEW)
                c == :
                    Button(,=c,=)\
                        .grid(=rindex+,=cindex+,=EW)
                :
                    Button(,=c,=)\
                        .grid(=rindex+,=cindex,=NSEW)



__name__ == :
    root = Tk()
    root.geometry()
    root.title()
    app = Application(=root)
    root.mainloop()

二,运行结果显示为“=”不跨行,如下图:

image.png

我检查了代码,没有发现有什么问题,程序也没有报错,我看到提问区也有人在问这个问题,但是没有看到老师就此问题的回答。

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2119楼
Python 全系列/第二阶段:Python 深入与提高/模块 2120楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2123楼

1.png

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

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

一、代码如下:

"""测试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编程(隐藏) 2127楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2128楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2129楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2130楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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