小言_互联网的博客

如何使用Python给图像算法做个简单应用界面

316人阅读  评论(0)

以前在Windows上做界面用MFC,现在做算法都是基于Python,所以转用Python的Tkinter库来做。主要是能使用Opencv和Torch处理数据,然后在界面上显示。

效果如下:

主要包括3个板块,其余还有一些小功能:

1、显示固定的图片。或从电脑加载一张图片并显示(涉及到按钮的响应函数编写和弹窗)

2、下拉框和文本框的使用

3、进度条的使用(涉及到多线程)

     

Tkinter支持控件自动调整布局,但是时间比较赶就不研究了,使用固定位置布局,界面也不给调整。

控件名称

Buttom 按钮,软件交互功能实现

Label (叫什么不重要),用来显示图片或文字

ComboBox 下拉框,做选择

Entry 文本框,做文本输入

Progressbar 进度条,算法跑起来之后显示进度

LabelFrame (...),灰色的框框,模块化布局控件

 

代码如下:


  
  1. import tkinter as tk
  2. import tkinter.ttk as ttk
  3. import tkinter.messagebox
  4. import tkinter.filedialog
  5. import cv2 as cv
  6. from PIL import Image, ImageTk
  7. import time
  8. import threading
  9. RELIEF=[ 'flat', 'raised', 'sunken', 'solid', 'ridge', 'groove']
  10. CURSOR=[ 'arrow', 'circle', 'clock', 'cross', 'dotbox', 'exchange',
  11. 'fleur', 'heart', 'man', 'mouse', 'pirate', 'plus',
  12. 'shuttle', 'sizing', 'spider', 'spraycan', 'star', 'target',
  13. 'tcross', 'trek', 'watch']
  14. def PIL2CV(im):
  15. im = im[:, :, :: -1]
  16. return ImageTk.PhotoImage(Image.fromarray(im))
  17. def Buttom1_CallBack():
  18. filename = tk.filedialog.askopenfilename() #弹出文件选择对话框
  19. if filename== '': #用户没有选择任何文件
  20. return
  21. new_img = cv.imread(filename)
  22. if new_img is None:
  23. tk.messagebox.showerror( '抱歉', '图片加载失败!')
  24. return
  25. new_img = cv.resize(new_img, ( 130, 120))
  26. new_img = PIL2CV(new_img)
  27. #后面两句实现图片切换显示
  28. Label2.configure(image=new_img, width= 130, height= 120)
  29. Label2.image = new_img
  30. tk.messagebox.showinfo( '提示', '加载图片完成!')
  31. def Buttom2_CallBack():
  32. info = Combobox1.get()
  33. param = Entry1.get()
  34. tk.messagebox.showwarning( '警告', '你选择了:'+info+ ' '+param)
  35. def process_code(delay):
  36. for i in range( 100):
  37. Progressbar1[ 'value'] = i+ 1
  38. root.update()
  39. time.sleep(delay)
  40. Buttom3.configure(text= '开始处理', state= 'normal')
  41. tk.messagebox.showinfo( '提示', '处理完成!')
  42. Progressbar1.configure(value= 0)
  43. def Buttom3_CallBack():
  44. yn = tk.messagebox.askyesno( '警告', '是否需要开始处理?')
  45. if not yn:
  46. return
  47. Buttom3.configure(text= '处理中...', state= 'disabled') #控件失效
  48. delay = 0.01
  49. # 单独开一个线程,绑定线程函数process_code,参数后面的','很关键
  50. # 不开线程界面会进入处理函数死循环,用户体验不太好
  51. t = threading.Thread(target=process_code, args=(delay,))
  52. t.start()
  53. def Buttom4_CallBack():
  54. global page_count
  55. if page_count<= 0:
  56. page_count = 0
  57. return
  58. else:
  59. page_count -= 1
  60. Label4.configure(text= '第'+str(page_count)+ '页')
  61. return
  62. def Buttom5_CallBack():
  63. global page_count
  64. if page_count>= 100:
  65. page_count = 100
  66. return
  67. else:
  68. page_count += 1
  69. Label4.configure(text= '第' + str(page_count) + '页')
  70. return
  71. #上面是控件的响应函数
  72. ################################################################################
  73. #下面是界面控件的布局
  74. #主界面
  75. root = tk.Tk()
  76. root.title( 'python界面测试') #修改界面标题
  77. root.iconbitmap( 'img/tm.ico') #修改界面ico
  78. root.geometry( '800x500') #设定界面尺寸 HxW
  79. root.resizable(width= False, height= False) #不允许调整窗口大小,不固定删除此行
  80. #添加两个板块边界框
  81. Frame1 = tk.LabelFrame(root, height= 200, width= 145)
  82. Frame1.place(x= 15, y= 100)
  83. Frame2 = tk.LabelFrame(root, text= "结果显示", height= 400, width= 620)
  84. Frame2.place(x= 170, y= 5)
  85. #添加图片显示框、加载图片框、加载图片按钮
  86. img = cv.imread( 'img/title.jpg') #opencv加载图片
  87. img = cv.resize(img, ( 140, 70)) #图片缩放
  88. img = PIL2CV(img) #opencv格式转pillow
  89. Label1 = tk.Label(root, image=img) #初始化默认图片
  90. Label1.place(x= 15, y= 20) #图片显示框在界面上的位置
  91. Label2 = tk.Label(root,
  92. width= 18,height= 7, #控件大小(注意单位不是像素)
  93. bg= "white") #默认白色背景
  94. Label2.place(x= 20,y= 110) #图片显示框在界面上的位置
  95. Buttom1 = tk.Button(root,
  96. width= 15,height= 1, #按钮大小
  97. text= '加载检索图片', #按钮文本
  98. relief=RELIEF[ 3], #按钮的风格
  99. command=Buttom1_CallBack) #绑定响应函数
  100. Buttom1.place(x= 25, y= 250) #按钮在界面上的位置
  101. #添加参数文本框、下拉框、下拉框内容输出按钮
  102. Combobox1 = ttk.Combobox(root, width= 17, height= 1)
  103. Combobox1[ 'value'] = ( '窗前明月光', '疑是地上霜', '举头望明月', '明月照我影')
  104. Combobox1.current( 0)
  105. Combobox1.place(x= 15, y= 320)
  106. Label3 = tk.Label(root, text= '参数')
  107. Label3.place(x= 15, y= 350)
  108. Entry1 = ttk.Entry(root, width= 9) #文本框为啥没有H
  109. Entry1.place(x= 50, y= 350)
  110. Entry1.insert( 0, '0.5')
  111. Buttom2 = tk.Button(root,
  112. width= 15,height= 1,
  113. text= '你选择了什么?',
  114. relief=RELIEF[ 3],
  115. command=Buttom2_CallBack)
  116. Buttom2.place(x= 25, y= 380)
  117. #添加进度条、开始处理按钮
  118. Progressbar1 = ttk.Progressbar(root, length= 600, value= 0, cursor=CURSOR[ 1])
  119. Progressbar1.place(x= 15, y= 460)
  120. Buttom3 = tk.Button(root,
  121. width= 15,height= 1,
  122. text= '开始处理',
  123. relief=RELIEF[ 3],
  124. command=Buttom3_CallBack)
  125. Buttom3.place(x= 630, y= 455)
  126. #添加两个滚动按钮
  127. Buttom4 = tk.Button(root,
  128. width= 3,height= 1,
  129. text= '<',
  130. relief=RELIEF[ 1],
  131. command=Buttom4_CallBack)
  132. Buttom4.place(x= 380, y= 410)
  133. global page_count #全局变量,用来控制页码
  134. page_count= 0
  135. Label4 = tk.Label(root, text= '第0页')
  136. Label4.place(x= 420, y= 410)
  137. Buttom5 = tk.Button(root,
  138. width= 3,height= 1,
  139. text= '>',
  140. relief=RELIEF[ 1],
  141. command=Buttom5_CallBack)
  142. Buttom5.place(x= 470, y= 410)
  143. root.mainloop()
  144. #这句话后面不能有代码

 


转载:https://blog.csdn.net/XLcaoyi/article/details/116270230
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场