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

一、程序

'''测试Lable'''
from tkinter import *
from tkinter import messagebox
class Application(Frame):
    def __init__(self,master=None):
        '''调用Frame构造方法'''
        super().__init__(master)#这一行的master和下一行的master有什么联系?master有什么具体含义
        self.master=master
        self.pack()#调用布局管理器,这一行的作用是什么?好像没有需要显示的内容
        self.creatWidget()#调用创建的组件,为什么要在定义类属性里边调用creatWidget?
    def creatWidget(self):
        self.w1=Text(root,width=40,height=12,bg='gray')
        self.w1.pack()
        self.w1.insert(1.0,'012345678\nabcdefg')
        self.w1.insert(2.3,'锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦\n')
        Button(self,text='重复插入文本',command=self.insertText).pack(side='left')
        Button(self, text='返回文本', command=self.returnText).pack(side='left')
    def insertText(self):
        self.w1.insert(INSERT,'Gaoqi')
        self.w1.insert(END,'[sxt]')
        self.w1.insert(1.8, 'gaoqi')
    def returnText(self):
        print(self.w1.get(1.2,1.6))
        print('所有文本内容:\n'+self.w1.get(1.0,END))

if __name__=='__main__':
    root=Tk()
    root.geometry('400x100+200+300')
    root.title('一个经典的Gui程序类测试')
    app=Application(master=root)
    root.mainloop()

二、问题

blob.png

老师self这个东西有点傻傻分不清楚了,老师可以适当点拨一下吗?本程序中self全程代表createwidget吗?

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

一、程序

'''测试Lable'''
from tkinter import *
from tkinter import messagebox
class Application(Frame):
    def __init__(self,master=None):
        '''调用Frame构造方法'''
        super().__init__(master)#这一行的master和下一行的master有什么联系?master有什么具体含义
        self.master=master
        self.pack()#调用布局管理器,这一行的作用是什么?好像没有需要显示的内容
        self.creatWidget()#调用创建的组件,为什么要在定义类属性里边调用creatWidget?
    def creatWidget(self):
        '''创建登录界面的组件'''
        self.label01=Label(self,text='用户名')
        self.label01.pack()

        v1=StringVar()
        self.entry01=Entry(self,textvariable=v1)
        self.entry01.pack()
        v1.set('admin')#疑问1按照程序的执行顺序应该是从上往下执行,这一行代码不应该往上移动两行吗,先设定v1值
        print(v1.get())
        '''创建密码框'''
        self.label02 = Label(self, text='密码')
        self.label02.pack()

        v2 = StringVar()
        self.entry02 = Entry(self, textvariable=v2)
        self.entry02.pack()
        print(v2.get())
        Button(self,text='登录',command=self.login).pack()#疑问2,这个地方为什么不是self.Button
    def login(self):
        username=self.entry01.get()
        pwd=self.entry02.get()

        if username=='gaoqi' and pwd=='123456':
            messagebox.showinfo('尚学堂学习系统','登录成功')
        else:
            messagebox.showinfo('尚学堂学习系统','登录失败')

if __name__=='__main__':
    root=Tk()
    root.geometry('400x100+200+300')
    root.title('一个经典的Gui程序类测试')
    app=Application(master=root)
    root.mainloop()

二、疑问

blob.png老师我的疑问就是箭头指向的地方,谢谢老师解答

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2224楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2225楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2226楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2227楼
Python 全系列/第二阶段:Python 深入与提高/模块 2230楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2232楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2233楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2234楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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