栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python中的TKinter(一)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python中的TKinter(一)

课程作业:使用任意一种语言,制作一个GUI界面,并基于GUI界面完成一些对图片的基本操作。
由于对python语言使用的比较多,记录一下学习TKinter的过程,方便以后查看。

(图片来源于菜鸟教程)

下面的每一个代码块的说明。

import tkinter

top = tkinter.Tk()
top.mainloop()

首先是import tkinter这个包,这里要注意t不大写。按照我的理解,GUI界面实质上的不停闪烁的界面,只是人眼不能看出界面的闪烁,我们才看到一个界面的浑然一体。
所以首先用top表示tkinter来简化代码,节省时间,然后建立一个循环,让这个界面一直闪烁即一直出现。
.

top.title("图片处理")
top.geometry("500x600")

这里加上两个常用的属性说明。也就是标题和创建窗口的大小。但是需要注意的是,这些内容都应该在top.mainloop()前面,top.mainloop()放在最后,应该是最后才会刷新。
.

def button1():                                                             
    fpath = filedialog.askopenfilename()                                   
    img = Image.open("%s"%fpath)                                           
    global img_open                                                        
    img_open = ImageTk.PhotoImage(img)                                     
    label_img = tkinter.Label(top, image=img_open)                         
    label_img.place(x=50, y=150)                                           
b = tkinter.Button(top, text='加载图片', width=10, height=2, command= button1) 
b.place(x=10, y=10)    # 按钮位置                                              

首先要创建一个按钮,设置按钮的名字,按钮的大小宽度,和按了按钮之后要发生的事件。
事件对应的是一个函数。
函数中先浏览并选中本地的图片,通过PIL中的Image打开。全局定义一个变量,这个变量就是把image打开的图片转化成tkinter可以接受的格式。之后建立一个标签,用来展示图片即可。
这样一个简单的tkinter加载图片的GUI就实现了。

因为老师还没有具体要求要什么功能,所以这里就先这样,后面要求添加什么功能了,在继续写。

附上全部代码

import tkinter                                                                
from tkinter import filedialog                                                
from PIL import Image, ImageTk                                                
                                                                              
top = tkinter.Tk()                                                            
top.title("图片处理")                                                             
top.geometry("800x600")                                                       
                                                                              
def button1():                                                                
    fpath = filedialog.askopenfilename()                                      
    img = Image.open("%s"%fpath)                                              
    global img_open                                                           
    img_open = ImageTk.PhotoImage(img)                                        
    label_img = tkinter.Label(top, image=img_open)                            
    label_img.place(x=50, y=150)                                              
b = tkinter.Button(top, text='加载图片', width=10, height=2, command= button1)    
b.place(x=10, y=10)    # 按钮位置                                                 
                                                                              
top.mainloop()                                                                
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/767470.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号