tkinter简单使用

import tkinter as tk
from PIL import Image, ImageTk
from tkinter.filedialog import askopenfilename
from objectIdentify import get_result


class ProjectFace:
    def __init__(self):
        self.text = tk.Text(window, width=58, height=30)
        self.t = tk.Text(window, width=58, height=13)
        self.scroll = tk.Scrollbar()

    def start(self):
        window.geometry('400x600')
        window.title('黄图像鉴定器')
        tk.Button(window, text='上传图片', command=pf.choiceImg).place(x=180, y=0)

    def resize(self, pil_image):  # 参数是:要适应的窗口宽、高、Image.open后的图片
        w, h = pil_image.size  # 获取图像的原始大小
        f1 = 1.0 * 400 / w
        f2 = 1.0 * 600 / h
        factor = min([f1, f2])
        width = int(w * factor)
        height = int(h * factor)
        return pil_image.resize((width, height), Image.ANTIALIAS), height

    def choiceImg(self):
        # 将滚动条填充
        msg = ''
        path = askopenfilename(title='选择文件')
        modify_before_imange = Image.open(path)
        modify_after_imange, height = self.resize(modify_before_imange)
        render = ImageTk.PhotoImage(modify_after_imange)
        self.text.image_create(tk.END, image=render)  # 用这个方法创建一个图片对象,并插入到“END”的位置
        img = tk.Label(image=render)
        img.image = render
        self.text.place(x=0, y=50)
        msg += get_result(path)
        self.showResult(msg, height)

    def showResult(self, msg, height):
        # 将滚动条填充
        self.scroll.pack(side=tk.RIGHT, fill=tk.Y)  # side是滚动条放置的位置,上下左右。fill是将滚动条沿着y轴填充
        self.t.pack(side=tk.LEFT, fill=tk.Y)  # 将文本框填充进wuya窗口的左侧,
        # 将滚动条与文本框关联
        self.scroll.config(command=self.t.yview)  # 将文本框关联到滚动条上,滚动条滑动,文本框跟随滑动
        self.t.config(yscrollcommand=self.scroll.set)  # 将滚动条关联到文本框
        self.t.insert(tk.END, msg)
        self.t.insert(tk.END, '\n')
        self.t.insert(tk.END, '\n')
        self.t.insert(tk.END, '\n')
        self.t.place(x=0, y=height+10)

if __name__=='__main__':
    window = tk.Tk()
    pf = ProjectFace()
    pf.start()
    tk.mainloop()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值