使用tkinter开发图形界面显示图片(Win10,Python)

liftword5个月前 (02-16)技术文章48

Tkinter(也叫Tk接?)是Tk图形?户界??具包标准的Python接?。Tk是?个轻量级的跨平台图形?户界?(GUI)开发?具。

Tkinter是Python自带的官方标准库,安装Python之后直接导入就可以使用,我们常见的python IDLE就是使用TKinter实现的。

在Win10命令行窗口输入python、import tkinter、dir(tkinter)、help(tkinter),可获得tkinter的相关帮助信息。

help(tkinter)命令的执行结果,列出了模块帮助文档的链接:
https://docs.python.org/3.10/library/tkinter.html

实现打开并显示图片的源代码如下:


import tkinter as tk

import tkinter.filedialog

from PIL import Image,ImageTk

#选择并显示图片

def choosepic():

path_ = tkinter.filedialog.askopenfilename()

path.set(path_)

print(path)

img_open = Image.open(entry.get())

#img = ImageTk.PhotoImage(img_open.resize((200,200)))

img = ImageTk.PhotoImage(img_open)

lableShowImage.config(image=img)

lableShowImage.image = img

if __name__ == '__main__':

#生成tk界面 app即主窗口

app = tk.Tk()

#修改窗口titile

app.title("显示图片")

#设置主窗口的大小和位置

app.geometry("800x400+200+200")

#Entry widget which allows displaying simple text.

path = tk.StringVar()

entry = tk.Entry(app, state='readonly', text=path,width = 100)

entry.pack()

#使用Label显示图片

lableShowImage = tk.Label(app)

lableShowImage.pack()

#选择图片的按钮

buttonSelImage = tk.Button(app, text='选择图片', command=choosepic)

buttonSelImage.pack()

#buttonSelImage.pack(side=tk.BOTTOM)

#Call the mainloop of Tk.

app.mainloop()


假设将源代码保存在文件“d:\temp\MyShowImage.py”。在命令行执行命令

python d:\temp\MyShowImage.py

程序运行,界面如下图所示

Tkinter(也叫Tk接?)是Tk图形?户界??具包标准的Python接?。Tk是?个轻量级的跨平台图形?户界?(GUI)开发?具。

Tkinter是Python自带的官方标准库,安装Python之后直接导入就可以使用,我们常见的python IDLE就是使用TKinter实现的。

在Win10命令行窗口输入python、import tkinter、dir(tkinter)、help(tkinter),可获得tkinter的相关帮助信息。

help(tkinter)命令的执行结果,列出了模块帮助文档的链接:
https://docs.python.org/3.10/library/tkinter.html

实现打开并显示图片的源代码如下:

import tkinter as tk

import tkinter.filedialog

from PIL import Image,ImageTk

#选择并显示图片

def choosepic():

path_ = tkinter.filedialog.askopenfilename()

path.set(path_)

print(path)

img_open = Image.open(entry.get())

#img = ImageTk.PhotoImage(img_open.resize((200,200)))

img = ImageTk.PhotoImage(img_open)

lableShowImage.config(image=img)

lableShowImage.image = img

if __name__ == '__main__':

#生成tk界面 app即主窗口

app = tk.Tk()

#修改窗口titile

app.title("显示图片")

#设置主窗口的大小和位置

app.geometry("800x400+200+200")

#Entry widget which allows displaying simple text.

path = tk.StringVar()

entry = tk.Entry(app, state='readonly', text=path,width = 100)

entry.pack()

#使用Label显示图片

lableShowImage = tk.Label(app)

lableShowImage.pack()

#选择图片的按钮

buttonSelImage = tk.Button(app, text='选择图片', command=choosepic)

buttonSelImage.pack()

#buttonSelImage.pack(side=tk.BOTTOM)

#Call the mainloop of Tk.

app.mainloop()


假设将源代码保存在文件“d:\temp\MyShowImage.py”。在命令行执行命令

python d:\temp\MyShowImage.py

程序运行,界面如下图所示


点击“选择图片”按钮,弹出打开文件对话框,

选择要打开的图片文件,点击“打开”按钮,图片就会显示在图形界面上了,如下图所示:



上述代码实现了图片的选择和显示功能,但也有不足之处,比如“没有滚动条,无法完全显示比屏幕大的图片”。

相关参考网页

(1)
https://blog.csdn.net/xyzhan/article/details/113856833
(2021年开发Python图形用户界面(GUI)的6种最佳Python GUI框架)

(2)
https://wenku.baidu.com/view/8f03733bf9d6195f312b3169a45177232e60e44d.html
(推荐8款常?的PythonGUI图形界?开发框架)

(3)https://github.com/honghaier-game/TKinterDesigner (honghaier-game/TKinterDesigner

(4)
https://blog.csdn.net/HG0724/article/details/112248635
(Python之Tkinter进行GUI开发)

(5)
https://blog.csdn.net/wj0807/article/details/120890111
在tkinter中显示图片和图片名

(6)
https://www.cnpython.com/qa/58793
(如何在gui中使用tkinter显示图像)

(7)
https://m.php.cn/article/419452.html
(python idle是什么)

(8)https://docs.python.org/3.10/library/tkinter.html (tkinter — Python interface to Tcl/Tk)

(9)
https://blog.csdn.net/tyler880/article/details/106862322?spm=1001.2101.3001.6650.1&utm_medium=
distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-106862322-blog-112248635.pc_relevant_aa&depth_1-utm_source=
distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-106862322-blog-112248635.pc_relevant_aa&utm_relevant_index=2 (
python GUI编程(Tkinter)

(10)
https://www.cnpython.com/qa/308976
(用滚动条在Tkinter中显示多个图像)

相关文章

Python大佬手把手教你用wxPython模块编写界面程序

wxPython是一个开发桌面端图形界面的跨平台函数库,开发语言为Python,它是基于C++的函数库wxWidgets的封装。私信小编01即可获取Python学习资料wxpython有大量组件,它们...

Dear PyGui:高性能的 Python 图形界面库

Dear PyGui 是一个高性能的 Python 图形用户界面(GUI)库,它基于 Dear ImGui 框架开发,主要用于构建实时图形应用和工具。与许多传统的 Python GUI 库不同,Dea...

用python做界面开发用什么好?_用python做界面设计

界面开发指的是什么?是web界面还是桌面应用开发?Web界面应用开发Python的web界面应用开发实现其实也是通过使用前端技术配合tpl语法实现的,可以介绍常用的web开发框架:1、Django:D...

Python Qt GUI设计:UI界面可视化组件、属性概述(基础篇—4)

Qt Designer中有很多UI界面组件,每种组件相对应有可设置的属性,本篇博文来让我们简单了解一下吧~1、界面组件在Qt Designer中,提供了八大类界面可视化组件分别为:布局组件(Layou...

这些漂亮的 GUI 是用 Python 构建的

让我们来看看用 Python 构建的一些漂亮的 GUI!注册页面这是一个 Tkinter Designer 项目,这个特定的 GUI 也是使用 Tkinter 框架和 Figma 设计器构建的。如果您...

python qt5 编写gui界面 基于qt库,简单的主界面编写。#软件开发

接着上一节,于是搞出了第一段程序,写一个gi程序,窗口八百乘六百使用qt,于是这里就进入了真正的gui开发。gui开发用了pvqt五,它是伏洛姆库,导入这些东西,导入之后就有一个coscost的温度,...