会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132359个问题
Python 全系列/第二阶段:Python 深入与提高/模块 271楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 272楼

一、代码如下:

"""测试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编程(隐藏) 273楼

老师,为什么我找着视频中的源码敲了一遍然后,保存所写的文本时总是报错。。





"""开发记事本软件的菜单

"""

coding=utf-8


from tkinter.filedialog import *

from tkinter.colorchooser import *

from tkinter import *




class Application(Frame):


    def __init__(selfmaster=None):

        super().__init__(master)        # super()代表的是父类的定义,而不是父类对象

        self.master = master

        self.textpad = None             # textpad表示Text文本框对象

        self.pack()

        self.createWidget()


    def createWidget(self):

        # 创建主菜单栏

        menubar = Menu(root)


        # 创建子菜单

        menuFile = Menu(menubar)

        menuEdit = Menu(menubar)

        menuHelp = Menu(menubar)


        # 将子菜单加入到主菜单栏

        menubar.add_cascade(label="文件(F)"menu=menuFile)

        menubar.add_cascade(label="编辑(E)"menu=menuEdit)

        menubar.add_cascade(label="帮助(H)"menu=menuHelp)


        # 添加菜单项

        menuFile.add_command(label="新建"accelerator="ctrl+n"command=self.newfile)

        menuFile.add_command(label="打开"accelerator="ctrl+o"command=self.openfile)

        menuFile.add_command(label="保存"accelerator="ctrl+s",command=self.savefile)

        menuFile.add_separator()  # 添加分割线

        menuFile.add_command(label="退出"accelerator="ctrl+q",command=self.exit)


        # 将主菜单栏加到根窗口

        root["menu"] = menubar


        # 增加快捷键的处理

        root.bind("<Control-n>",lambda event:self.newfile())

        root.bind("<Control-o>",lambda event:self.openfile())

        root.bind("<Control-s>",lambda event:self.savefile())

        root.bind("<Control-q>",lambda event:self.exit())


        #文本编辑区

        self.textpad = Text(root, width=50, height=30)

        self.textpad.pack()


        # 创建上下菜单

        self.contextMenu = Menu(root)

        self.contextMenu.add_command(label="背景颜色"command=self.openAskColor)


        #为右键绑定事件

        root.bind("<Button-3>",self.createContextMenu)


    def newfile(self):

        self.textpad.delete("1.0""end")  # 把text控件中所有的内容清空

        self.filename= asksaveasfilename(title="另存为",initialfile="未命名.txt",

                          filetypes=[("文本文档","*.txt")],

                          defaultextension=".txt")

        self.savefile()


    def openfile(self):

        self.textpad.delete("1.0","end")        # 把text控件中所有的内容清空

        with askopenfile(title="打开文本文件"as f:

            self.textpad.insert(INSERT,f.read())

            self.filename = f.name


    def savefile(self):

        with open(self.filename,"w"as f:

            c = self.textpad.get(1.0,END)

            f.write(c)


    def exit(self):

        root.quit()


    def openAskColor(self):

        s1 = askcolor(color="red",title="选择背景色")

        self.textpad.config(bg=s1[1])

    def createContextMenu(self,event):

        # 菜单在鼠标右键单击的坐标处显示

        self.contextMenu.post(event.x_root, event.y_root)



if __name__ == '__main__':

    root = Tk()

    root.geometry("450x300+200+300")

    root.title("百战程序员的简易记事本")

    app = Application(master=root)

    root.mainloop()

截图如下:

屏幕截图 2021-04-11 173723.png

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 274楼
Python 全系列/第二阶段:Python 深入与提高/模块 275楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 276楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 277楼

老师这为啥报错了???


from tkinter import *

from tkinter import messagebox



class Application(Frame):   #继承Frame类,(容器)

    def __init__(self,master=None):  #构造函数,传入root对象

        super().__init__(master)    #调用Frame类的构造函数

        self.master = master

        self.pack()    

        

        self.creatwidget()    #调用creatwidgrt方法

        self.creatwidget2()


    def creatwidget(self):   #定义creatwidget方法,用于标签

        self.label01 = Label(self,text="百战",width=10,height=3,bg="black",fg="white"#标题,宽度,高度,背景色,前景色

        self.label01.pack()


        self.label02 = Label(self,text="程序",width=15,height=2,bg="blue",fg="white")

        self.label02.pack()


        self.label03 = Label(self,text="123456\n你是猪吧",borderwidth=1,relief="solid",justify="right"#标题,边界,实线,对齐

        self.label03.pack()


    def creatwidget2(self):  #定义用于生成按钮

        self.btn01 = Button(self,text="点击就送花",command=self.songhua)

        self.btn01.pack()


        self.btnoff = Button(self,text="关闭",command=root.destroy)

        self.btnoff.pack()


    def songhua(self):  #定义送花方法

        messagebox.showinfo("送花","送你一朵玫瑰花")


    global photo

    photo = PhotoImage(file="C:/Users/。。/Desktop/python/GUI编程/photo/a.gif"#图片,.gif

    self.label04 = Label(self,image=photo)

    self.label04.pack()  

        

            



root = Tk()  #创建root对象

root.geometry("500x800+300+200")  #调整窗口大小

root.title("GUI经典")    #窗口标题

app = Application(master=root)  #实力化app对象

root.mainloop()  



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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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