Python self-study-class15-UI interface design and application enhancement

1. Interface optimization:
Insert picture:

#coding=gbk
import tkinter

win = tkinter.Tk()
photo = tkinter.PhotoImage(file=r"D:\壁纸\游戏\1.gif")
label = tkinter.Label(win,text="芜湖",image=photo)
label.pack()
win.mainloop()

A little question: I found that python3 can only insert .gif but not .jpg, and the actual display after inserting the .gif is static. I wonder if anyone can answer it? ? ?
Set the font:

import tkinter

win = tkinter.Tk()
label = tkinter.Label(win,text="UI界面",
                      font=("华文彩云",80),
                      fg="yellow",
                      bg="blue") #字体类型,大小,字体颜色,背景
label.pack()
win.mainloop()

2. Interface layout:
absolute layout:

import tkinter
win = tkinter.Tk()
label1 = tkinter.Label(win,text="1234",bg="blue")
label2 = tkinter.Label(win,text="1234",bg="yellow")
label3 = tkinter.Label(win,text="1234",bg="red")

#坐标位置的绝对布局,窗口位置大小不影响布局
label1.place(x=10,y=10)
label2.place(x=50,y=50)
label3.place(x=100,y=100)
'''
label1.place(x=10,y=10,anchor=tkinter.SE)
label2.place(x=50,y=50,anchor=tkinter.SE)
label3.place(x=100,y=100,anchor=tkinter.SE)
'''
win.mainloop()

Relative layout:

import tkinter
win = tkinter.Tk()
label1 = tkinter.Label(win,text="1234",bg="blue")
label2 = tkinter.Label(win,text="1234",bg="yellow")
label3 = tkinter.Label(win,text="1234",bg="red")

label1.pack(fill=tkinter.Y,side=tkinter.LEFT) #fill,窗口变化,y同步变化
label2.pack()
label3.pack()
win.mainloop()

In addition, there are table layouts. These types of layouts are quite confusing to learn. If you use them in the future, you need to think carefully.
3. Binding events:
mouse events:

import tkinter
def call(event):
    print(event.x,event.y)  #显示鼠标位置
win = tkinter.Tk()
frame = tkinter.Frame(win,width=200,height=200) #框架,限定范围,不显示
frame.bind("<Button-1>",call)#鼠标左键
frame.bind("<Button-3>",call)#鼠标右键
frame.pack()
win.mainloop()

Keyboard events:

import tkinter
def call(event):
    print(event.keysym)  #显示键盘值
win = tkinter.Tk()
frame = tkinter.Frame(win,width=200,height=200) #框架,限定范围,不显示
frame.bind("<Key>",call)  #激发函数
frame.focus_set()   #获取焦点
frame.pack()
win.mainloop()

Monitor the position of the mouse in the window:

import tkinter
def call(event):
    print(event.x,event.y)  #显示鼠标位置
win = tkinter.Tk()
frame = tkinter.Frame(win,width=200,height=200) #框架,限定范围,不显示
frame.bind("<Motion>",call)#鼠标位置
frame.pack()
win.mainloop()

4. Graphical programming (the last time the data search visualization is based on deepening)
mainly adds saving, and the choice of three (List, Table, Text) search methods
1) Input display function (inputview):

#coding=gbk
import tkinter
from tkinter import ttk
import 图形化编程.BigDataFind
import 图形化编程.BigDataSave
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.button1 = tkinter.Button(self.win,text = "搜索",command = self.search)  #导入搜索键,command表示绑定search的行为
        self.button1.place(x=200,y=0)
        self.frame = tkinter.Frame(self.win)  # 框架,限定范围,不显示
        self.frame.bind("<Key>", self.call)  # 激发函数
        self.frame.focus_set()  # 获取焦点
        self.frame.pack()
        self.button2 = tkinter.Button(self.win,text = "保存",command = self.save)  #导入搜索键,command表示绑定search的行为
        self.button2.place(x=300,y=0)

        self.comvalue = tkinter.StringVar()  # 窗体自带文本,新建一个值
        self.comboxlist = ttk.Combobox(self.win, textvariable=self.comvalue,width=100)  # 初始化
        self.comboxlist["values"] = ("listshow", "testshow", "tableshow")
        self.comboxlist.current(0)  # 选择第一个
        self.comboxlist.bind("<<ComboboxSelected>>", self.go)  # 绑定事件与函数
        self.comboxlist.place(x=0,y=50)
        self.howtoshow="listshow"

        self.comvaluefile = tkinter.StringVar()  # 窗体自带文本,新建一个值
        self.comboxlistfile = ttk.Combobox(self.win, textvariable=self.comvaluefile,width=100)  # 初始化
        self.comboxlistfile["values"] = (r"D:\Python代码\class15\图形化编程\txm.txt",
                                         r"D:\Python代码\class15\图形化编程\bigdata.txt",
                                         r"D:\Python代码\class15\图形化编程\tmy.txt")
        self.comboxlistfile.current(0)  # 选择第一个
        self.comboxlistfile.bind("<<ComboboxSelected>>", self.filego)  # 绑定事件与函数
        self.comboxlistfile.place(x=0,y=80)
        self.howtoshowfile=r"D:\Python代码\class15\图形化编程\txm.txt"

    def go(self,*args):
        self.howtoshow=self.comboxlist.get()  #保存选中的值

    def filego(self, *args):
        self.howtoshowfile = self.comboxlistfile.get()  # 保存选中的值

    def call(self,event):
        print(event.keysym)
        if(event.keysym=="return"):
            self.search()

    def search(self):
        print("start search",self.entry.get())  #在运行窗口输出搜索的内容
        big=图形化编程.BigDataFind.bigdatafind(self.howtoshowfile,self.howtoshow)
        big.find(self.entry.get())
        big.show()
    def save(self):
        print("start search",self.entry.get())  #在运行窗口输出搜索的内容
        big=图形化编程.BigDataSave.bigdatasave(self.howtoshowfile,self.howtoshow)
        big.save(self.entry.get())
        big.show()
    def show(self):
        self.win.mainloop()

inputs=InputView()
inputs.show()

2) Data search function (BigDataFind):

#coding=gbk
import codecs   #数据编码解码
import 图形化编程.ListShow
import 图形化编程.TableShow
import 图形化编程.TextShow
class bigdatafind:
    def __init__(self,path,howtoshow):
        self.file = codecs.open(path,"rb","GBK","ignore")  #打开文件
        self.howtoshow = howtoshow
        self.showview = None  #窗体——创建

        if self.howtoshow=="listshow":
            self.showview=图形化编程.ListShow.Listshowdata()
        elif self.howtoshow=="textshow":
            self.showview=图形化编程.TextShow.Testshowdata()
        else:
            self.showview=图形化编程.TableShow.Tableshowdata()

    def find(self,searchstr):
        while True:
            line = self.file.readline()  #按行读入
            if line.find(searchstr) != -1:  #非空则显示
                print(line,end="")  #显示数据
                #插入
                if self.showview != None:
                    self.showview.addata(line)  # 显示窗体

            if not line:  #没有数据即退出
                break

    def show(self):
        if self.showview != None:
            self.showview.show()  #显示窗体
    def __del__(self):
        self.file.close()
'''
#test
big = bigdatafind("D:\\Python代码\\class15\\图形化编程\\bigdata.txt","show")
big.find("txm")
big.show()
'''

4) Data save function (BigDataSave):

#coding=gbk
import codecs   #数据编码解码
import 图形化编程.ListShow
import 图形化编程.TableShow
import 图形化编程.TextShow
class bigdatasave:
    def __init__(self,path,howtoshow):
        self.file = codecs.open(path,"rb","GBK","ignore")  #打开文件
        self.howtoshow = howtoshow
        self.showview = None  #窗体——创建

        if self.howtoshow=="listshow":
            self.showview=图形化编程.ListShow.Listshowdata()
        elif self.howtoshow=="textshow":
            self.showview=图形化编程.TextShow.Testshowdata()
        else:
            self.showview=图形化编程.TableShow.Tableshowdata()

    def save(self,searchstr):
        baconFile = open(searchstr+'.txt', 'w')
        while True:
            line = self.file.readline()  #按行读入
            if line.find(searchstr) != -1:  #非空则显示
                baconFile.write(line)
                if self.showview != None:
                    self.showview.addata(line)  # 显示窗体
            if not line:  #没有数据即退出
                baconFile.close()
                break

    def show(self):
        if self.showview != None:
            self.showview.show()  #显示窗体
    def __del__(self):
        self.file.close()

5) Three ways to display functions (ListShow, TableShow, TextShow):

import tkinter
class Listshowdata:
    def __init__(self):
        self.win=tkinter.Tk() #构造窗体
        self.win.geometry("800x800+300+0")   #搜索数据显示窗口
        self.mylist=tkinter.Listbox(self.win,width=200 )  #列表框
        self.mylist.pack()
    def addata(self,inserstr):
        self.mylist.insert(tkinter.END,inserstr)
        pass
    def show(self):
        self.win.mainloop()
#coding=gbk
import tkinter
from tkinter import ttk
class Tableshowdata:
    def __init__(self):
        self.win=tkinter.Tk() #构造窗体
        self.win.geometry("800x800+300+0")   #搜索数据显示窗口
        self.tree = ttk.Treeview(self.win)   #表格
        self.tree.pack()
        self.idnum=0  #标志插入的位置

        self.tree["columns"] = ("user","password","email")
        self.tree.column("user",width=200)  #表示列,不显示
        self.tree.column("password",width=200)
        self.tree.column("email",width=200)

        self.tree.heading("user",text="CSDN-name")  #显示表头
        self.tree.heading("password",text="CSDN-password ")
        self.tree.heading("email",text="CSDN-email")

    def addata(self,inserstr):  #数据插入
        datalist = inserstr.split(" # ")
        if len(datalist)==3:
            self.tree.insert("",self.idnum,text="line"+str(self.idnum+1),values=(datalist[0],datalist[1],datalist[2]))#插入的行数
            self.idnum+=1

    def show(self):
        self.tree.pack()
        self.win.mainloop()
#coding=gbk
import tkinter
class Testshowdata:
    def __init__(self):
        self.win=tkinter.Tk() #构造窗体
        self.win.geometry("800x800+300+0")   #搜索数据显示窗口
        self.text=tkinter.Text(self.win)  #文本编辑器
        self.text.pack()
    def addata(self,inserstr):
        self.text.insert(tkinter.INSERT,inserstr)
        pass
    def show(self):
        self.win.mainloop()

Running effect:
click search to get it,
Insert picture description here
click save to save as txm0.txt
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46837674/article/details/113249276