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

"""测试Text多行文本框组件的基本用法,使用面向对象的方式"""

from tkinter import *
import webbrowser


class Application(Frame):

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

    def createWidget(self):
        self.w1 = Text(root, width=40, height=12, bg='gray')
        self.w1.pack()

        self.w1.insert(1.0, '123456789\n987654321')
        self.w1.insert(2.3, '锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦\n')

        Button(self, text='重复插入文本', command=self.insertText).pack(side='left')
        Button(self, text='返回文本', command=self.returnText).pack(side='left')
        Button(self, text='添加图片', command=self.addImage).pack(side='left')
        Button(self, text='添加组件', command=self.addWidget).pack(side='left')
        Button(self, text='通过tag精确控制文本', command=self.testTag).pack(side='left')

    def insertText(self):
        # INSERT索引表示在光标处插入
        self.w1.insert(INSERT, 'gaoqi')
        # END索引表示在最后插入
        self.w1.insert(END, 'sxt')

    def returnText(self):
        # Indexes(索引)是用来指向Text组件中文本的位置,Text的组件索引也是对应实际字符之间的位置。
        # 核心:行号以1开始,列号以0开始
        print(self.w1.get(1.2, 1.6))
        print('文本所有内容:\n', self.w1.get(1.0, END))

    def addImage(self):
        self.photo=PhotoImage(file='imgs/logo.gif')
        self.w1.image_create(END, image=self.photo)

    def addWidget(self):
        b1 = Button(self.w1, text='爱尚学堂')
        # 在text创建组件的命令
        self.w1.window_create(INSERT, window=b1)

    def testTag(self):
        self.w1.delete(1.0, END)
        self.w1.insert(INSERT, 'good good study day day up\n百战程序员\n北京尚学堂\n百度一下你就知道')
        self.w1.tag_add('good', 1.0, 1.9)
        self.w1.tag_config('good', background='yellow', foreground='red')

        self.w1.tag_add('百度', 4.0, 4.2)
        self.w1.tag_config('百度', underline=True)
        self.w1.tag_bind('百度', '<Button-1>', self.webshow)

    def webshow(self, event):
        webbrowser.open('http://www.baidu.com')


if __name__ == '__main__':
    root = Tk()
    root.geometry("450x300+200+300")
    app = Application(master=root)
    root.mainloop()

老师我的运行之后,五个button在下面,没有在顶部是什么原因呢?

image.png

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

"""测试Label组件的基本用法,使用面向对象的方式"""

from tkinter import *


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

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

    def createWidget(self):
        """创建组件"""
        self.label01 = Label(self, text='百战程序员', width=10, Height=2,
                             bg='black', fg='white')
        self.label01.pack()

        self.label02 = Label(self, text='高淇老师', width=10, Height=2,
                             bg='blue', fg='white', font=('黑体', 30))
        self.label02.pack()

        # 显示图像
        global photo      # 把global声明成全局变量
        photo = PhotoImage(file='imgs/logo.gif')
        self.label03 = Label(self, image=photo)
        self.label03.pack()

        # 显示多行文本
        self.label04 = Label(self, text='北京尚学堂\n百战程序员\n老高真帅',
                             borderwidth=1, relief='solid', justify='right')
        self.label04.pack()


if __name__ == '__main__':
    root = Tk()
    root.geometry('400x240+200+300')
    app = Application(master=root)
    root.mainloop()

老师帮忙看下哪里有问题

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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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