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

from tkinter import *              #定义了图形用户界面(GUI)模块
from tkinter import messagebox     
 
 
class Application(Frame):          #定义一个应用类(Application)它的父类是一个容器(Frame)
    """一个经典的GUI程序的类的写法"""
 
    def __init__(self,master=None): # 构造器用来构造属性
        super().__init__(master)    # Frame不会自动调用父类所以需要super(),super代表的是父类的定义,而不是父类的对象
        self.master=master
        self.pack()
        self.createWidget()
 
    def createWidget(self):
        """创建组件"""
        self.bth01=Button(self)
        self.bth01["text"]="点击送花"
        self.bth01.pack()
        self.bth01["command"]=self.songhua
        # 创建一个退出按钮
 
 
        self.bth01Quit=Button(self, text="退出", command=root.destroy)#  destroy它是指销毁所用的子类、派生类以及销毁此小部件和所有子部件
        self.bth01Quit.pack()
 
    def songhua(self):
        messagebox.showinfo("送花","送你99朵玫瑰花")
 
 
if __name__ == '__main__':
 
    root=Tk()#  根窗口对象
    root.geometry("400x100+200+300")#  窗口大小
    root.title("一个经典的GUI程序类的测试")#建立窗口标题
    app=Application(master=root)
    root.mainloop()

请问老师:

                1.面向对象的写法不用bind方法吗?它不用绑定吗

                2.为什么还要再加一个实例属性 self.master 呢

                3.定义方法后边的self 是不是也能写成Application呢

                

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

老师,我总是报下面的错误,该怎么解决,我百度了但是没找到什么有效地方法

# coding=utf-8
"""开发记事本软件,使用面向对象的编程方式"""
from tkinter import *
from tkinter.filedialog import *
from tkinter.colorchooser import *


class Application(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.textPad = None
        self.filename = None
        self.Contextmenu = None
        self.pack()
        self.creatwidget()

    def creatwidget(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)

        # 在文件菜单下添加选项
        # 都可以通过command参数绑定方法,此处省略
        # accelerator是快捷键参数
        menuFile.add_command(label="新建N", accelerator="ctrl+n")
        menuFile.add_command(label="打开O", accelerator="ctrl+o",
                             command=self.openfile)
        menuFile.add_command(label="保存S", accelerator="ctrl+s",
                             command=self.savefile)
        # 添加一道下滑线
        menuFile.add_separator()
        menuFile.add_command(label="退出", accelerator="ctrl+q",
                             command=self.exit)

        # 添加上下文菜单,即右键快捷菜单
        self.Contextmenu = Menu(self.master)
        self.Contextmenu.add_command(label="选择背景颜色", command=self.choosebgcolor)
        self.master.bind("<Button-3>", self.ms)

        # 把主菜单添加到主窗口
        root.config(menu=menubar)
        self.textPad = Text(self.master, width=300, height=300, bg="gray")
        self.textPad.pack()

        # 绑定快捷键
        root.bind("<Control-o>", self.openfile)
        root.bind("<Control-s>", self.savefile)
        root.bind("<Control-q>", self.exit)
        root.bind("<Control-n>", self.newfile)

    def ms(self, event):
        self.Contextmenu.post(event.x_root, event.y_root)

    def exit(self, event=None):
        self.master.quit()

    def openfile(self, event=None):
        self.textPad.delete(1.0, END)
        with askopenfile(title="打开文本文件", filetypes=[("文本文件", ".txt")]) as file:
            self.filename = file.name
            self.textPad.insert(INSERT, file.read())

    def savefile(self, event=None):
        with open(self.filename, "w") as file:
            c = self.textPad.get(1.0, END)
            file.write(c)

    def newfile(self, event=None):
        self.textPad.delete(1.0, END)
        self.filename = asksaveasfilename(title="新建文件",
                                          initialfile="未命名.txt", filetypes=[(
                "文本文件", ".txt")], defaultextension=".txt")

    def choosebgcolor(self):
        colors = askcolor(color="gray", title="选择背景颜色")
        self.textPad.config(bg=colors[1])


if __name__ == "__main__":
    root = Tk()
    root.geometry('500x500')
    app = Application(root)

    root.mainloop()

image.png

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 627楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 629楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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