在Tkinter GUI中显示Python脚本的输出
我是python新手&请求社区专家的帮助。我试图在Tkinter GUI中显示以下脚本的输出。我遵循了StackOverflow上提供的许多解决方案,但不幸的是,我无法在代码中实现这些解决方案。我需要帮助在Tkinter GUI中显示以下脚本的输出。所以我可以在Tkinter小部件中显示输出在Tkinter GUI中显示Python脚本的输出,python,opencv,user-interface,image-processing,tkinter,Python,Opencv,User Interface,Image Processing,Tkinter,我是python新手&请求社区专家的帮助。我试图在Tkinter GUI中显示以下脚本的输出。我遵循了StackOverflow上提供的许多解决方案,但不幸的是,我无法在代码中实现这些解决方案。我需要帮助在Tkinter GUI中显示以下脚本的输出。所以我可以在Tkinter小部件中显示输出 import os import tkinter as tk import cv2 import numpy as np from PIL import Image from PIL import Imag
import os
import tkinter as tk
import cv2
import numpy as np
from PIL import Image
from PIL import ImageTk
class Difference_Button(Sample_Button, Button):
def on_click_diff_per_button(self, diff_per):
threshold = 0.8 # set threshold
resultsDirectory = 'Data/Differece_images'
sourceDirectory = os.fsencode('Data/images')
templateDirectory = os.fsencode('Data/Sample_images')
detectedCount = 0
for file in os.listdir(sourceDirectory):
filename = os.fsdecode(file)
if filename.endswith(".jpg") or filename.endswith(".png"):
print(filename)
img = cv2.imread('Data/images/' + filename)
im_grayRef = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
for templateFile in os.listdir(templateDirectory):
templateFilename = os.fsdecode(templateFile)
if filename.endswith(".jpg") or filename.endswith(".png"):
Sample_image = cv2.imread('Data/Sample_images/' + templateFilename, 0)
#im_graySam = cv2.cvtColor(Sample_image, cv2.COLOR_BGR2GRAY)
cv2.waitKey(0)
w, h = Sample_image.shape[::-1]
score = cv2.matchTemplate(im_grayRef,Sample_image,cv2.TM_CCOEFF_NORMED)
#diff = (diff * 255).astype("uint8")
cv2.waitKey(0)
diffvalue = print("Image similarity %ge of_" + filename + "_&_" + templateFilename + "_is", score * 100)
# res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(score >= threshold)
if (len(loc[0])):
detectedCount = detectedCount + 1
for pt in zip(*loc[::-1]):
cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
cv2.imwrite(resultsDirectory + '/diff_per_' + filename + '.jpg', img)
Difference_per_label.config(text='diff_per ' + filename + "_&_" + templateFilename + ' saved')
print('diff_per ' + filename + "_&_" + templateFilename + ' saved')
# break
#print('detected positive ' + str(detectedCount))
continue
else:
continue
if __name__ == '__main__':
root = tk.Tk()
root.title('Image GUI')
root.geometry('1280x960')
os.makedirs('Data/Differece_images', exist_ok=True)
pw_left = tk.Frame(root, relief='ridge', borderwidth=4)
pw_left.pack(side='left',anchor='nw')
pw_left = tk.Frame(root, relief='ridge', borderwidth=4)
pw_left.pack(side='left', anchor='nw')
frame7 = tk.Frame(pw_left, bd=2, relief="ridge")
frame7.pack()
difference_button = Difference_Button(root, frame7)
Difference_per_label = tk.Label(frame7, text='print', width=40, bg='white', height = '5')
Difference_per_label.pack(fill=tk.X)
Diff_label = tk.Label(frame7, textvariable= 'diffvalue', width=40, bg='white', height = '5')
Diff_label.pack(fill=tk.X)
Difference_button = tk.Button(frame7, text='Difference',
command=lambda: difference_button.on_click_diff_per_button(Difference_per_label))
Difference_button.pack(side='bottom', padx=5, pady=5)
root.mainloop()
需要帮助部分:
- 如何在Tkinter
diffvalue=print(“图像相似性%ge of_“+文件名+”、&“+模板文件名+”、is),分数*100)中显示以下命令输出
- 下面的命令未显示所有内容
从开始到结束的结果。它只显示了最后一个
输出
Difference_per_label.config(text='diff_per'+filename+“&&'+templateFilename+'saved')
- 完成后,我想为语句更正标签应用尝试除之外的逻辑,即当前显示为
和diffvalue=print(“图像相似性%ge of_u”+filename+“&&”+templateFilename+“u is”,分数*100)
这样,如果文件夹中没有任何内容,它将抛出异常命令打印('diffu per'+filename+“&”+templateFilename+“saved'))
- 图像相似性%ge(共个):已修复
- 文件名:变量
- _&_:固定
- templateFilename:变量
- _是:固定的
- 分数*100:根据差异百分比而变化
print()
仅在屏幕上发送文本。它从不返回显示的文本。要分配给变量,请在不使用print()的情况下使用它
diffvalue = "Image similarity %ge of_{}_&_{}_is {}".format(filename, templateFilename, score * 100)
现在您可以在标签
或文本
或列表框
要将文本附加到标签
中,您必须从标签
中获取旧文本,将新文本连接到旧文本,然后将所有内容再次放入标签
-即
new_text = 'diff_per {}_&_{} saved'.format(filename, templateFilename)
Difference_per_label["text"] = Difference_per_label["text"] + "\n" + new_text
或更短的代码+=
Difference_per_label["text"] += "\n" + new_text
因为当您更改标签中的文本时,tkinter
(和其他GUI)不会更新窗口中的小部件,但当它结束按钮执行的函数并返回mainloop时,它会返回mainloop,所以您可能必须在更改标签中的文本后使用root.update()
,以强制mainloop()
重新绘制窗口中的Windget
要抛出异常,您需要raise()
,而不是try/except
,它用于捕获异常
要测试是否没有文件,您必须从os.listdir()
as列表获取所有数据,使用endswith()
过滤列表,并检查列表是否为空
import os
sourceDirectory = '.'
all_files = os.listdir(sourceDirectory)
all_files = [os.fsdecode(file) for file in all_files]
#all_files = map(os.fsdecode, all_files)
all_files = [file for file in all_files if file.lower().endswith((".jpg",".png"))]
#if len(all_files) == 0:
if not all_files:
raise(Exception("No Files"))
print()
只在屏幕上发送文本。它从不返回显示的文本。要分配给变量,请在不使用print()的情况下使用它
diffvalue = "Image similarity %ge of_{}_&_{}_is {}".format(filename, templateFilename, score * 100)
现在您可以在标签
或文本
或列表框
要将文本附加到标签
中,您必须从标签
中获取旧文本,将新文本连接到旧文本,然后将所有内容再次放入标签
-即
new_text = 'diff_per {}_&_{} saved'.format(filename, templateFilename)
Difference_per_label["text"] = Difference_per_label["text"] + "\n" + new_text
或更短的代码+=
Difference_per_label["text"] += "\n" + new_text
因为当您更改标签中的文本时,tkinter
(和其他GUI)不会更新窗口中的小部件,但当它结束按钮执行的函数并返回mainloop时,它会返回mainloop,所以您可能必须在更改标签中的文本后使用root.update()
,以强制mainloop()
重新绘制窗口中的Windget
要抛出异常,您需要raise()
,而不是try/except
,它用于捕获异常
要测试是否没有文件,您必须从os.listdir()
as列表获取所有数据,使用endswith()
过滤列表,并检查列表是否为空
import os
sourceDirectory = '.'
all_files = os.listdir(sourceDirectory)
all_files = [os.fsdecode(file) for file in all_files]
#all_files = map(os.fsdecode, all_files)
all_files = [file for file in all_files if file.lower().endswith((".jpg",".png"))]
#if len(all_files) == 0:
if not all_files:
raise(Exception("No Files"))
print()
只在屏幕上发送文本。它从不返回显示的文本。若要分配给变量,请在不使用print()
-diffvalue=“Image…”
的情况下使用它。若要将文本附加到标签
,则必须从标签
获取旧文本,将新文本附加到旧文本,将所有内容再次放在标签
上。顺便说一句:只有当问题没有用时,我们才关闭问题(即,有其他问题需要解决,或者没有足够的信息来解决,等等),而不是在所有问题都得到解决时。其他:如果循环中的conitunue
之后没有其他代码,print()
仅在屏幕上发送文本。它从不返回显示的文本。若要分配给变量,请在不使用print()
-diffvalue=“Image…”的情况下使用它
如果您想将文本附加到标签
上,那么您必须从标签
中获取旧文本,将新文本附加到旧文本,然后将所有内容再次添加到标签
顺便说一句:只有当问题没有用时(即,有其他问题有解决方案,或者没有足够的信息来解决它,等等),我们才会关闭问题,而不是在所有问题都得到解决的情况下。否则:如果循环中的conitunue
之后没有其他代码,那么在的末尾继续和继续是没有用的。非常感谢您的支持。diffvalue=“图像相似性%ge of{}{}{}{{}u是{}”。格式(文件名、模板文件名、分数*100)
和每个标签的差异[“text”]+=“\n”+new_text
对您很有帮助。您能帮我更新窗口中的小部件吗?以及如何测试目录中是否没有文件?我添加了代码,检查是否没有图像并引发错误您想更新什么小部件?要更新标签中的文本,您有每个标签的差异[“text”]=…
。其他类似的小部件小部件[“变量”]=value
或