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

"""测试一个GUI程序的写法,使用面向对象的方式"""
# coding=utf-8

from tkinter import *
from tkinter import messagebox


class Application(Frame):
    """一个经典的GUI程序的类的写法"""

    def __init__(self, master=None):
        super().__init__(master)
        # super代表的是父类的定义、而不是父类的对象
        self.master = master
        self.pack()
        self.createWidget()

    def createWidget(self):
        """创建组件"""
        self.btn01 = Button(self, text="点击送花", command=self.songhua,
                            anchor=CENTER, width=6, height=3)
        self.btn01.pack()

        # 创建一个退出按钮
        self.btnQuit = Button(self, text="退出", command=root.destroy)
        self.btnQuit.pack()

    def songhua(self):
        messagebox.showinfo("送花", "送你99朵玫瑰花")
        # 需要调用messagebox组件、“送花”是这个组件的抬头、后面的是内容。


if __name__ == '__main__':
    root = Tk()
    root.geometry("800x500+300+300")
    root.title("一个经典的GUI程序类的测试")
    root.update()
    app = Application(master=root)
    root.mainloop()
"""测试一个经典的GUI程序的写法、用面向对象的方法"""
# coding=utf-8
from tkinter import *
from tkinter import messagebox


class Application(Frame):
    """一个经典的GUI程序的"""

    def __init__(self,master = None):
        super().__init__(master)
        self.master = master
        self.createWidget()


    def createWidget(self):
        self.btn01 = Button(text="登录",command=self.login,width=6)
        self.btn01.pack()

    def login(self):
        messagebox.showinfo("登录","登录成功!")



if __name__ == '__main__':
    root = Tk()
    root.geometry("600x500+200+200")
    root.update()
    app = Application(master=root)
    root.mainloop()

老师:上面两段代码、第一段代码在构造函数处加了self.pack(),如果不加这个的话、按钮就不显示。

而第二段代码、即便在构造函数处不加self.pack(),也可以运行成功,这是为什么?能帮忙查看一下原因么?

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

一、代码如下:

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编程(隐藏) 2096楼
Python 全系列/第二阶段:Python 深入与提高/模块 2097楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2100楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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