Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在网格中显示图像的缩略图,然后使用tkinter和python在新窗口中打开一个图像?_Tkinter_Python 3.7_New Window_Image Enlarge - Fatal编程技术网

如何在网格中显示图像的缩略图,然后使用tkinter和python在新窗口中打开一个图像?

如何在网格中显示图像的缩略图,然后使用tkinter和python在新窗口中打开一个图像?,tkinter,python-3.7,new-window,image-enlarge,Tkinter,Python 3.7,New Window,Image Enlarge,更新:找到解决方案并更新为自我回答。 我在一个目录中有20个jpg图像。使用Python 3.7+Tkinter 8.6.10。我访问它们,调整大小并将它们显示在网格中的主窗口中。网格有12行x 5列=60帧。 第1行中的所有帧都有标签,标签中填充了大小调整后的图像。 第2行中的所有框架都有带有文本“放大”的标签。 第3行中的所有框架都有带有文本“Select”的标签。 这种模式重复 单击“放大”按钮,我想在新窗口中显示完整的未调整大小的图像。我在显示放大图像时遇到了一个问题。将打开一个新窗口

更新:找到解决方案并更新为自我回答。

我在一个目录中有20个jpg图像。使用Python 3.7+Tkinter 8.6.10。我访问它们,调整大小并将它们显示在网格中的主窗口中。网格有12行x 5列=60帧。 第1行中的所有帧都有标签,标签中填充了大小调整后的图像。 第2行中的所有框架都有带有文本“放大”的标签。 第3行中的所有框架都有带有文本“Select”的标签。 这种模式重复

单击“放大”按钮,我想在新窗口中显示完整的未调整大小的图像。我在显示放大图像时遇到了一个问题。将打开一个新窗口,但没有图像。我在控制台中遇到以下错误: --------------------控制台输出开始--------------------

(ce7comb1)rohit@rohitu2004lts:~/PyWDUbuntu/thesis$python3 test\u gui\u 3.py 图像数量=20 排列= [home/rohit/PyWDUbuntu/thesis/imgs2detect20imgs/371903.jpg',/home/rohit/PyWDUbuntu/thesis/imgs2detect20imgs/2192573.jpg',“/home/rohit/PyWDUbuntu/thesis/imgs2detect20imgs/1989609.jpg',”/home/rohit/PyWDUbuntu/thet/thet/detect20imgs/1283466.jpg',“/home/home/rohit/PyWDUbuntu/PyWDUbuntu/detect/1897.jpg'/Imgs2Detect_20imgs/1624481.jpg',“/home/rohit/PyWDUbuntu/thesis/Imgs2Detect_20imgs/36979.jpg',”/home/rohit/PyWDUbuntu/thesis/Imgs2Detect_20imgs/148284.jpg',“/home/rohit/PyWDUbuntu/thet/thesis/Imgs2Detect_20imgs/2209751.jpg',“/home/rohit/PyWDUbuntu/thesis/Imgs2Detect/1440465.jpg',”,“/home/pygs2detect/PyWDUbuntu/imgs9162.jpg'“/home/rohit/PyWDUbuntu/thesis/imgs2detect20imgs/2069887.jpg”、“/home/rohit/PyWDUbuntu/thesis/imgs2detect20imgs/1243756.jpg”、“/home/rohit/PyWDUbuntu/thesis/imgs2detect20imgs/2148982.jpg”、“/home/rohit/PyWDUbuntu/detect20imgs/984950.jpg”、“/home/therohit/PyWDUbuntu/theus/thesis/Imgs2Detect/1317;/pywdg、/home/romg”is/Imgs2Detect_20imgs/1920465.jpg','/home/rohit/PyWDUbuntu/thesis/Imgs2Detect_20imgs/1254659.jpg','/home/rohit/PyWDUbuntu/thesis/Imgs2Detect_20imgs/2209317.jpg','/home/rohit/PyWDUbuntu/thesis/Imgs2Detect_20imgs/371902.jpg']

n_行=12 n_列=5

Tkinter回调中的异常 回溯(最近一次呼叫最后一次): 文件“/home/rohit/anaconda3/envs/ce7comb1/lib/python3.7/tkinter/init.py”,第1705行,在调用中 返回self.func(*args) 文件“test\u gui\u 3.py”,第120行,在do\u放大\u btn\u按\u功能中 图像=图像(原始) 文件“/home/rohit/anaconda3/envs/ce7comb1/lib/python3.7/tkinter/init.py”,第2766行,在init 小部件。init(自、主、“标签”、cnf、kw) 文件“/home/rohit/anaconda3/envs/ce7comb1/lib/python3.7/tkinter/init.py”,第2299行,在init (widgetName,self._w)+额外+自选项(cnf)) _tkinter.TclError:映像“pyimage21”不存在

--------------------控制台输出端--------------------

这是python脚本的完整代码,我不知道我做错了什么

# importing required packages
import tkinter as tk
from PIL import ImageTk, Image
import os

img_folder = r'/home/rohit/PyWDUbuntu/thesis/Imgs2Detect_20imgs/'

image_files_list = [os.path.join(img_folder, f) \
                    for f in os.listdir(img_folder) \
                    if os.path.isfile(os.path.join(img_folder, f))]
print(f"num of images = {len(image_files_list)}\narray=\n{image_files_list}")

class c_my_wnd_main_window:
    def __init__(self, _wnd_name):
        self.wnd = tk.Tk()
        self.wnd_name = _wnd_name
        self.frame_arr = None
    

def build_frames(self, _n_rows, _n_cols, _in_img_arr):
    ## create array holding skeleton form of the objects of required frame types
    self.frame_arr = []
    for r_idx in range(_n_rows):
        self.wnd.columnconfigure(r_idx, weight=1, minsize=50)
        self.wnd.columnconfigure(r_idx, weight=1, minsize=50)
        temp_array = []
        for c_idx in range(_n_cols):
            frame_name_str = ''.join( ['frm_', str(r_idx+1), '_', str(c_idx+1)] )
            if (r_idx % 3) == 0: ## picture frame
                temp_array.append(my_frm_image(self.wnd, frame_name_str))
            elif (r_idx % 3) == 1: ## enlarge frame
                temp_array.append(my_frm_enlarge(self.wnd, frame_name_str))
            else: ## select frame
                temp_array.append(my_frm_select(self.wnd, frame_name_str))
        self.frame_arr.append(temp_array)
    
    ## set the frame arrays objects as required
    img_idx = 0
    for r_idx in range(_n_rows):
        for c_idx in range(_n_cols):
            if (r_idx) % 3 == 0: ## picture frame
                self.frame_arr[r_idx][c_idx].frm_img.grid(row=r_idx,
                                                          column=c_idx,
                                                          padx=5, pady=5
                                                         )
                ## populate the image
                resized_image, file_with_path = opened_imgs_arr[img_idx]
                self.frame_arr[r_idx][c_idx].add_image(resized_image)
                img_idx += 1
                self.frame_arr[r_idx][c_idx].image_with_path = file_with_path
            elif (r_idx % 3) == 1: ## enlarge frame
                self.frame_arr[r_idx][c_idx].frm_enlarge.grid(row=r_idx,
                                                          column=c_idx,
                                                          padx=5, pady=5
                                                         )
                self.frame_arr[r_idx][c_idx].btn_enlarge.pack(padx=5, pady=5)
                self.frame_arr[r_idx][c_idx].enlarge_this_image = self.frame_arr[r_idx - 1][c_idx].image_with_path ## bcoz immediately previous row should reference the image frame
            else: ## select frame
                self.frame_arr[r_idx][c_idx].frm_select.grid(row=r_idx,
                                                          column=c_idx,
                                                          padx=5, pady=5
                                                         )
                self.frame_arr[r_idx][c_idx].btn_select.pack(padx=5, pady=5)

class my_frm_image:
    def __init__(self, _wnd, _frame_name):
        self.frm_img = tk.Frame(
            master=_wnd,
            relief=tk.SUNKEN,
            borderwidth=2)
        self.frame_name = _frame_name
        self.image_with_path = None
        self.lbl_img = tk.Label(master=self.frm_img, image=None)
        
def add_image(self, _in_img):
    #print(f" function add_image called ")
    self.lbl_img.configure(image=_in_img)
    self.lbl_img.pack(padx=1, pady=1)

class my_frm_enlarge:
    def __init__(self, _wnd, _frame_name):
        self.frm_enlarge = tk.Frame(
            master=_wnd,
            relief=tk.RAISED,
            borderwidth=4)
        self.frame_name = _frame_name
        self.enlarge_this_image = None
        self.btn_enlarge = tk.Button(
            master=self.frm_enlarge,
            text=f"Enlarge",
            bg="black", fg="white", 
            command=self.do_enlarge_btn_press_functionality
        )
        self.btn_enlarge.pack(padx=5, pady=5)
    

def do_enlarge_btn_press_functionality(self):
    #print(f"Would have enlarged image: {self.enlarge_this_image}")
    img_orig = ImageTk.PhotoImage(Image.open(self.enlarge_this_image))
    wnd_enlarged_img = tk.Tk()
    frm_enlarged_img = tk.Frame(
        master=wnd_enlarged_img,
        relief=tk.SUNKEN,
        borderwidth=2
    )
    #lbl_enlarged_img_path = tk.Label(master=wnd_enlarged_img, text=self.enlarge_this_image)
    #lbl_enlarged_img_path.pack()
    lbl_enlarged_img = tk.Label(
        master=wnd_enlarged_img,
        image=img_orig)
    lbl_enlarged_img.pack(padx=1, pady=1)
    frm_enlarged_img.pack()
    wnd_enlarged_img.mainloop()

class my_frm_select:
    def __init__(self, _wnd, _frame_name):
        self.frm_select = tk.Frame(
            master=_wnd,
            relief=tk.RAISED,
            borderwidth=4)
        self.frame_name = _frame_name
        self.btn_select = tk.Button(
            master=self.frm_select,
            text=f"Select",
            bg="black", fg="white",
            command=self.do_select_btn_press_functionality
        )
        self.btn_select.pack(padx=5, pady=5)
    

def do_select_btn_press_functionality(self):
    #print(f"Pressed Select button: {self.frame_name}")
    if self.btn_select["text"] == 'Select':
        ## Sink button and change text to Deselect
        self.btn_select.configure(text=f"Deselect", relief=tk.SUNKEN, bg="yellow", fg="black")
    else:
        ## Raise button and change text to Select
       self.btn_select.configure(text=f"Select", relief=tk.RAISED, bg="black", fg="white")
    return


o_main_window = c_my_wnd_main_window('main_window')

opened_imgs_arr = []
for img_idx in range(20):
    img_orig = Image.open(image_files_list[img_idx])
    img_resized = img_orig.resize((100, 80),Image.ANTIALIAS)
    ## append tuple of resized image and file with path
    opened_imgs_arr.append( (ImageTk.PhotoImage(img_resized), image_files_list[img_idx]) )
    del img_orig, img_resized

num_rows_of_images = 4
n_rows = num_rows_of_images * 3
n_cols = int(len(image_files_list) / num_rows_of_images)
print(f"n_rows = {n_rows}\t\tn_cols = {n_cols}")
o_main_window.build_frames(n_rows, n_cols, image_files_list)
o_main_window.wnd.mainloop()

print(f"\n\nNormal exit.\n\n")

请帮助。

有几件事我在概念上还没有弄清楚,还有一些其他错误:如何和为什么使用顶级窗口与根窗口;如何将参数传递给按钮调用的函数;在某些地方指定窗口标题的愚蠢错误


如果对任何人都有帮助,请在此处将完整的清理代码上载到github:

这是因为您使用了多个
Tk()
实例。使用
Toplevel()
代替
do\u放大\u btn\u按功能()
。谢谢。顺便说一句,我刚刚破解了它。我在理解上有几个差距。发布工作和清洁代码作为答案。