我是靠谱客的博主 结实朋友,这篇文章主要介绍有关Python的图形化界面——tkinter,现在分享给大家,希望可以做个参考。

文档:

https://docs.python.org/zh-cn/3/library/tkinter.html

https://wiki.python.org/moin/TkInter

文档中的代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import tkinter as tk class Application(tk.Frame): def __init__(self, master=None): super().__init__(master) self.master = master self.pack() self.create_widgets() def create_widgets(self): self.hi_there = tk.Button(self) self.hi_there["text"] = "Hello Worldn(click me)" self.hi_there["command"] = self.say_hi self.hi_there.pack(side="top") self.quit = tk.Button(self, text="QUIT", fg="red", command=self.master.destroy) self.quit.pack(side="bottom") def say_hi(self): print("hi there, everyone!") root = tk.Tk() app = Application(master=root) app.mainloop()

执行结果(我给拉大了一点)

点QUIT就退出界面了

点”Hello World(click me)“会输出:hi there, everyone!

 

参考:https://www.runoob.com/python/python-gui-tkinter.html

属性描述
Dimension控件大小;
Color控件颜色;
Font控件字体;
Anchor锚点;
Relief控件样式;
Bitmap位图;
Cursor光标;
控件描述
Button按钮控件;在程序中显示按钮。
Canvas画布控件;显示图形元素如线条或文本
Checkbutton多选框控件;用于在程序中提供多项选择框
Entry输入控件;用于显示简单的文本内容
Frame框架控件;在屏幕上显示一个矩形区域,多用来作为容器
Label标签控件;可以显示文本和位图
Listbox列表框控件;在Listbox窗口小部件是用来显示一个字符串列表给用户
Menubutton菜单按钮控件,用于显示菜单项。
Menu菜单控件;显示菜单栏,下拉菜单和弹出菜单
Message消息控件;用来显示多行文本,与label比较类似
Radiobutton单选按钮控件;显示一个单选的按钮状态
Scale范围控件;显示一个数值刻度,为输出限定范围的数字区间
Scrollbar滚动条控件,当内容超过可视化区域时使用,如列表框。.
Text文本控件;用于显示多行文本
Toplevel容器控件;用来提供一个单独的对话框,和Frame比较类似
Spinbox输入控件;与Entry类似,但是可以指定输入范围值
PanedWindowPanedWindow是一个窗口布局管理的插件,可以包含一个或者多个子控件。
LabelFramelabelframe 是一个简单的容器控件。常用与复杂的窗口布局。
tkMessageBox用于显示你应用程序的消息框。

 

打开图片

参考:

https://zhuanlan.zhihu.com/p/75872830?from_voters_page=true

https://blog.csdn.net/u010921682/article/details/89848375?utm_term=tkinter%E6%89%93%E5%BC%80%E5%9B%BE%E7%89%87&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-2-89848375&spm=3001.4430

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from tkinter import * from tkinter import filedialog from PIL import Image, ImageTk import os window = Tk() window.title("Open picture") def clicked(): file = filedialog.askopenfilenames(initialdir=os.path.dirname(os.path.abspath(__file__))) return file file = clicked() filePosition = file[0] img = Image.open(filePosition) # 打开图片 photo = ImageTk.PhotoImage(img) # 用PIL模块的PhotoImage打开 imglabel = Label(window, image=photo) imglabel.grid(row=0, column=0, columnspan=3) window.mainloop()

执行结果:



灰度图像

参考:https://www.jb51.net/article/173339.htm

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from tkinter import * from tkinter import filedialog from PIL import Image, ImageTk import os window = Tk() window.title("Open picture") def clicked(): file = filedialog.askopenfilenames(initialdir=os.path.dirname(os.path.abspath(__file__))) return file file = clicked() filePosition = file[0] img = Image.open(filePosition) # 打开图片 img = img.convert('L') photo = ImageTk.PhotoImage(img) # 用PIL模块的PhotoImage打开 imglabel = Label(window, image=photo) imglabel.grid(row=0, column=0, columnspan=3) window.mainloop()

结果:

如果两张图片一起显示:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from tkinter import * from tkinter import filedialog from PIL import Image, ImageTk import os window = Tk() window.title("Open picture") def clicked(): file = filedialog.askopenfilenames(initialdir=os.path.dirname(os.path.abspath(__file__))) return file file = clicked() filePosition = file[0] img = Image.open(filePosition) # 打开图片 Img = img.convert('L') photo = ImageTk.PhotoImage(img) # 用PIL模块的PhotoImage打开 imglabel = Label(window, image=photo) imglabel.grid(row=0, column=0, columnspan=3) photo1 = ImageTk.PhotoImage(Img) # 用PIL模块的PhotoImage打开 imglabel = Label(window, image=photo1) imglabel.grid(row=0, column=3, columnspan=3) window.mainloop()

结果:

 

图片缩放

参考:

https://blog.csdn.net/guduruyu/article/details/70842142?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-2.control&dist_request_id=1328730.856.16167419643941449&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-2.control

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from tkinter import * from tkinter import filedialog from PIL import Image, ImageTk import os window = Tk() window.title("Open picture") def clicked(): file = filedialog.askopenfilenames(initialdir=os.path.dirname(os.path.abspath(__file__))) return file file = clicked() filePosition = file[0] img = Image.open(filePosition) # 打开图片 img = img.resize((256,128)) Img = img.convert('L') Img = Img.resize((256,128)) photo = ImageTk.PhotoImage(img) # 用PIL模块的PhotoImage打开 imglabel = Label(window, image=photo) imglabel.grid(row=0, column=0, columnspan=3) photo1 = ImageTk.PhotoImage(Img) # 用PIL模块的PhotoImage打开 imglabel = Label(window, image=photo1) imglabel.grid(row=0, column=3, columnspan=3) window.mainloop()

结果:

 

 

 

 

 

 

________________________________________________________________________

题外话,刚刚实操了一下加噪声(多亏我之前弯路走的足够多,现在连新模块都不用安装了,泪目)

照抄:https://blog.csdn.net/qq_38395705/article/details/106311905

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import cv2 import random import numpy as np from matplotlib import pyplot as plt def gasuss_noise(image, mean=0, var=0.001): ''' 添加高斯噪声 mean : 均值 var : 方差 ''' image = np.array(image / 255, dtype=float) noise = np.random.normal(mean, var ** 0.5, image.shape) out = image + noise if out.min() < 0: low_clip = -1. else: low_clip = 0. out = np.clip(out, low_clip, 1.0) out = np.uint8(out * 255) return out def sp_noise(image,prob): ''' 添加椒盐噪声 prob:噪声比例 ''' output = np.zeros(image.shape,np.uint8) thres = 1 - prob for i in range(image.shape[0]): for j in range(image.shape[1]): rdn = random.random() if rdn < prob: output[i][j] = 0 elif rdn > thres: output[i][j] = 255 else: output[i][j] = image[i][j] return output img = cv2.imread("starry-night.jpg") # 添加椒盐噪声,噪声比例为 0.02 out1 = sp_noise(img, prob=0.02) # 添加高斯噪声,均值为0,方差为0.01 out2 = gasuss_noise(img, mean=0, var=0.01) # 显示图像 titles = ['Original Image', 'Add Salt and Pepper noise','Add Gaussian noise'] images = [img, out1, out2] plt.figure(figsize = (20, 15)) for i in range(3): plt.subplot(1,3,i+1) plt.imshow(images[i],'gray') plt.title(titles[i]) plt.xticks([]),plt.yticks([]) plt.show()

结果:

 

 

另外,还有一个功能实现,见AttributeError: ‘Image‘ object has no attribute ‘shape‘

(打开文件之后,对文件进行加噪声的处理,再显示出来)

 

 

最后

以上就是结实朋友最近收集整理的关于有关Python的图形化界面——tkinter的全部内容,更多相关有关Python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部