简体   繁体   中英

Python Tkinter. Opening BMP File as Canvas

I am trying to load a BMP file as a canvas from my Python GUI using tkinter. I am able to access my current directory and choose which file I want, but I am only able to load the file I designated outside of my Window class (specified in my code below).

What the code does in general is load a BMP file as a canvas in order to designate 6 points onto the image, and create a best-fit ellipse using http://nicky.vanforeest.com/misc/fitEllipse/fitEllipse.html as reference.

Here is my code:

import tkinter as tk
from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageTk
import os

class Window(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)

        self.master = master
        self.pos = []
        self.master.title("BMP Image GUI")
        self.pack(fill=BOTH, expand=1)

        self.counter = 0

        menu = Menu(self.master)
        self.master.config(menu=menu)

        file = Menu(menu)
        file.add_command(label="Exit", command=self.client_exit)
        file.add_command(label="Open", command=self.openFile)
        menu.add_cascade(label="File", menu=file)
        analyze = Menu(menu)

        menu.add_cascade(label="Analyze", menu=analyze)

    def client_exit(self):
        exit()

    #Where I open my file
    def openFile(self):
        self.filename = filedialog.askopenfilename(initialdir=os.getcwd(), 
 title="Select BMP File", filetypes=[("BMP Files","*.bmp")])
        load = Image.open(self.filename)
        render = ImageTk.PhotoImage(load)

        img = Label(self, image=render)
        img.image = render
        img.place(x=0, y=0)
        w, h = load.size

        canvas = Canvas(root, width=w, height=h)
        canvas.create_image((w / 2, h / 2), image=tkimage)
        canvas.pack()

        root.geometry("%dx%d" % (w, h))

root = tk.Tk()

imgSize = Image.open("ap41.ddr.brf.sdat.bmp") # The only file the loads
tkimage = ImageTk.PhotoImage(imgSize)
w, h = imgSize.size

canvas = Canvas(root, width=w, height=h)

root.geometry("%dx%d" % (300, 300))
app = Window(root)
root.mainloop()
  • Don't use wildcard imports ( from module import * ). It leads to bugs and is against PEP8
  • Don't use quit() or exit() in real code. Those functions are meant for the REPL only.
  • Don't use "file" as a variable name since it's a built-in name in python2.
  • self.master = master is baked into all python widgets; you don't need to repeat it.
  • You need to create widgets once , and then update those widgets. You keep creating new widgets.

-

import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import os

class Window(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)

        menu = tk.Menu(self.master)
        master.config(menu=menu)

        file_menu = tk.Menu(menu)
        file_menu.add_command(label="Exit", command=self.quit)
        file_menu.add_command(label="Open", command=self.openFile)
        menu.add_cascade(label="File", menu=file_menu)
        analyze = tk.Menu(menu)

        menu.add_cascade(label="Analyze", menu=analyze)

        self.canvas = tk.Canvas(self)
        self.canvas.pack(fill=tk.BOTH, expand=True)
        self.image = None # none yet

    #Where I open my file
    def openFile(self):
        filename = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select BMP File", filetypes=[("BMP Files","*.bmp")])
        if not filename:
            return # user cancelled; stop this method

        load = Image.open(filename)
        w, h = load.size
        self.render = ImageTk.PhotoImage(load) #must keep a reference to this

        if self.image is not None: # if an image was already loaded
            self.canvas.delete(self.image) # remove the previous image

        self.image = self.canvas.create_image((w / 2, h / 2), image=self.render)

        root.geometry("%dx%d" % (w, h))

root = tk.Tk()
root.geometry("%dx%d" % (300, 300))
root.title("BMP Image GUI")
app = Window(root)
app.pack(fill=tk.BOTH, expand=1)
root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM