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 tkimport tkinter.filedialogfrom 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 = imgif __name__ == '__main__':#生成tk界面 app即主窗口app = tk.Tk()#修改窗口titileapp.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 tkimport tkinter.filedialogfrom 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 = imgif __name__ == '__main__':#生成tk界面 app即主窗口app = tk.Tk()#修改窗口titileapp.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中显示多个图像)
0 评论