Open a image file then display it in new window

2024/10/8 19:43:39

I have a button in my GUI then after selecting a image file I want it to display in new window.

Code:

import tkinter as tk
from tkinter import *   
from tkinter import ttk
from tkinter import filedialog
import os
from PIL import Image, ImageTk  # Place this at the end (to avoid any conflicts/errors)window = tk.Tk()
a = Tk()def openimgfile():currdir = os.getcwd()name = filedialog.askopenfile(initialdir = currdir, title = "Select a Image", filetype = ( ("PNG", "*.png"), ("JPEG", "*.jpg;.*jpeg"), ("All files", "*.*") ) )                         a.title("Pattern Matching")
a.minsize(200,200)
button1 = Button(text="Open file",width = 10,height =10,command=openimgfile).pack()a.mainloop()
Answer

For this to work you need to initialize this in command function:

from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageTk
import oswindow = Tk()def open_img_file():filename = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select file", filetypes=(("png images", ".png"), ("all files", "*.*")))if not filename:return# setup new windownew_window = Toplevel(window)# get imageimage = ImageTk.PhotoImage(Image.open(filename))# load imagepanel = Label(new_window, image=image)panel.image = imagepanel.pack()window.title("Pattern Matching")
window.minsize(200, 200)
button = Button(text="Open file", width=10, height=10,command=open_img_file)
button.pack()window.mainloop()
https://en.xdnf.cn/q/118679.html

Related Q&A

python plotting chart in interactive viewer vscode

This works and shows a plot in vscode: #%% cell with plot import matplotlib.pyplot as plt y = [3.2, 3.9, 3.7, 3.5, 3.02199] x = [0.15, 0.3, 0.45, 0.6, 0.75] n = [155, "outliner", 293, 230, 67…

Find all lines in a dataframe that matches specific pattern and extract the strings after split

I have a dataframe that looks like LineEntry: [0x0000000002758261-0x0000000002758268): /a/b/c:7921:14 LineEntry: [0x0000000002756945-0x0000000002756960): /f/b/c:6545:10 LineEntry: [0x00000000027562c9-0…

Python: Concatenate many dicts of numpy arrays with same keys and size

I have a function called within a loop that returns a dict (dsst_mean) with roughly 50 variables. All variables are numpy arrays of length 10.The loop iterates roughly 3000 times. Im current concatenat…

intersection of 2 objects of different types

i want to take the intersection of: ((e, 13.02338360095244), (a, 11.820318700775383), (o, 9.20172171683253), (s, 7.635081506807498), (n, 7.547469320471335), (i, 7.219915745772025), (r, 6.70492704072287…

Enemy Projectiles Attack Way To Fast Problem

I am trying to make my enemy bullets attack the player but its attacking way to fast I dont know why VIDEO my enemy bullets class# enemys bulletsksud = pygame.image.load("heart.png")class Boo…

How to find the maximum digit of an integer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.This p…

How to use python to generate a magazine cover? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Why do I get this error? NoneType object has no attribute shape in opencv

Im working on real-time clothing detection. so i borrowed the code from GitHub like this:https://github.com/rajkbharali/Real-time-clothes-detection but (H, W) = frame.shape[:2]:following error in last …

Efficiently concatenate two strings from tuples in a list?

I want to concatenate the two string elements in a list of tuplesI have this:mylist = [(a, b), (c, d), (e, f), (g, h)] myanswer = []for tup1 in mylist:myanswer.append(tup1[0] + tup[1])Its working but i…

How to assert that a function call does not return an error with unittest?

Is there anyway with unittest to just assert that a function call does not result in an error, whether it is a TypeError, IOError, etc.example:assert function(a,b) is not errororif not assertRaises fun…