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()                                                                
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值