1. Basics:
Learn how to call tkinter to create a form and add the required functions (buttons, text boxes, form background, etc.) to the created form
#coding=gbk
import tkinter
from tkinter import ttk
def go():
print("hello go")
win = tkinter.Tk() #构造窗体
win.title("hello python")
win.geometry("800x800+300+0")
button=tkinter.Button(win,text="点一下看看撒",command = go)#,width = 10,height=10)
button.pack() #加载到窗体
button1=tkinter.Button(win,text="点一下看看撒",command =lambda :print("你好呀"))
label = tkinter.Label(win,text = "hello python") #标签
button1.pack() #加载到窗体
#button.place(10,10)
newlabel = tkinter.Label(win,#父窗体
anchor = tkinter.CENTER, #位置
bg = "blue", #背景颜色
fg = "yellow", #标签颜色
text = "gogogi", #文本
width = 10,
height = 10)
newlabel.pack()
#文本输入框
entry = tkinter.Entry(win,width=50,bg="red",fg="black")
entry.pack()
#list显示查询数据
mylist = tkinter.Listbox(win,width=50) #列表框
mylist.pack()
for item in ["1","sasda","sdasd"]: #内容插入
mylist.insert(tkinter.END,item)
#数据选择查询
def go1(*args): #处理事件
print(comboxlist.get()) #选中当前的值
comvalue = tkinter.StringVar() #窗体自带文本,新建一个值
comboxlist = ttk.Combobox(win,textvariable=comvalue) #初始化
comboxlist["values"] = ("1","2","3","4")
comboxlist.current(0) #选择第一个
comboxlist.bind("<<ComboboxSelected>>",go1) #绑定事件与函数
comboxlist.pack()
win.mainloop() #进入消息循环
Running effect:
2. Basic application
of the form This part has learned some applications of the form and binding of events
#coding=gbk
import tkinter
import os
from tkinter import ttk
def go():
print(entry.get()) #返回输入的文本
if entry.get() == "记事本":
os.system("notepad")
win = tkinter.Tk()
entry = tkinter.Entry(win) #input
entry.pack()
button = tkinter.Button(win,text="搜索",command=go)
button.pack()
#文本编辑器
text = tkinter.Text(win)
text.insert(tkinter.INSERT,"hello")#插入文本
text.insert(tkinter.INSERT,"\r\n")#换行
text.insert(tkinter.INSERT,"hello")#插入文本
text.pack()
#树状
tree=ttk.Treeview(win) #表格
tree.pack()
tree["columns"] = ("姓名","年龄","身高")
tree.column("姓名",width=100) #表示列,不显示
tree.column("年龄",width=100)
tree.column("身高",width=100)
tree.heading("姓名",text="姓名") #显示表头
tree.heading("年龄",text="年龄") #显示表头
tree.heading("身高",text="身高") #显示表头
txm=tree.insert("",0,text="line1",values=("txm","21","176")) #插入具体值
tree.insert("",1,text="line2",values=("txm","21","176"))
txm1 = tree.insert(txm,1,text="line1",values=("tmm","21","176")) #创建树状结构
txm2 = tree.insert(txm,0,text="line2",values=("tmm","21","176")) #创建树状结构
win.mainloop()
Running effect:
3. Follow the tutorial to complete the advanced use of the form (data search visualization)
1) Because a larger txt data file is needed, I randomly created a
2) main function part (mainClass):
#coding=gbk
import 数据搜索可视化.inputview
import 数据搜索可视化.ListShowdata
import 数据搜索可视化.BigDataFind
myinput = 数据搜索可视化.inputview.InputView()
myinput.show()
'''
bigfind = 数据搜索可视化.BigDataFind.bigdatafind("D:\\Python代码\\class14\\数据搜索可视化\\bigdata.txt")
bigfind.find("tmy")
bigfind.show()
#myinput = 数据搜索可视化.inputview.InputView()
#myinput.show()
myshow = 数据搜索可视化.ListShowdata.showdatainlist()
myshow.adddata("ajskahdja")
myshow.show()
'''
3) Data Find Module (BigDataFind):
#coding=gbk
import codecs
import 数据搜索可视化.ListShowdata
class bigdatafind:
def __init__(self,path):
self.file = codecs.open(path,"rb","GBK","ignore") #打开文件
self.showdata = 数据搜索可视化.ListShowdata.showdatainlist()
def find(self,searchstr):
while True:
line = self.file.readline() #按行读入
if line.find(searchstr) != -1: #非空则显示
print(line,end="") #显示数据
self.showdata.adddata(line) #插入
if not line: #没有数据即退出
break
def show(self):
self.showdata.show()
4) Form input module (inputview):
#coding=gbk
import tkinter
import 数据搜索可视化.BigDataFind
class InputView:
def __init__(self):
self.win = tkinter.Tk()
self.win.geometry("800x800+300+0") #设置界面大小及位置
self.entry = tkinter.Entry(self.win) #导入文本框
self.entry.place(x=0,y=0)
self.button = tkinter.Button(self.win,text = "搜索",command = self.search) #导入搜索键,command表示绑定search的行为
self.button.place(x=200,y=0)
def search(self):
print("start search",self.entry.get()) #在运行窗口输出搜索的内容
bigfind = 数据搜索可视化.BigDataFind.bigdatafind(r"D:\Python代码\class14\数据搜索可视化\bigdata.txt") #数据搜索,对象绑定数据
bigfind.find(self.entry.get()) #find查找符合所搜索的文本
bigfind.show()
def show(self):
self.win.mainloop()
5) Data list display module (ListShowdata):
#coding=gbk
import tkinter
class showdatainlist:
def __init__(self):
self.win = tkinter.Tk()
self.win.geometry("800x800+300+0") #搜索数据显示窗口
self.list = tkinter.Listbox(self.win,width=800,height=800) #控制输出列表大小
self.list.pack()
def show(self):
self.win.mainloop()
def adddata(self,insertstr):
self.list.insert(tkinter.END,insertstr) #插入数据
6) The total function list: The
running effect is as follows:
Summary:
Finally, the data search visualization has made a very good summary of the basics of the previously learned form. Although it is just done by following the tutorial, a careful understanding can be familiar with the application design of the form , It can also strengthen the understanding and application of the relationship between classes;