代码语言:javascript复制
from tkinter import *
from tkinter import messagebox
class Application(Frame):
def __init__(self,master=None):
# Frame是父类,得主动的调用父类 的构造器
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()
# 在Text标签里面插入东西
self.w1.insert(1.0,"0123456789n abcdddd")
self.w1.insert(2.3,"ddddddddn")
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,"rrrr")
# END 表示在最后插入
self.w1.insert(END,'[kkk]')
def returnText(self):
print("所有的文本:" self.w1.get(1.0,END))
def addImage(self):
self.photo = PhotoImage(file = "imgs/1.gif")
self.w1.image_create(END,image=self.photo)
def addWidget(self):
b1 = Button(self.w1 ,text = "ffff")
self.w1.window_create(INSERT,window = b1)
root = Tk()
root.geometry("400x200 200 300")
root.title("测试")
app = Application(master = root)
root.mainloop()