我正在尝试从程序本身中加载任何图像,并将其显示在画布中。 一旦运行代码,就会出现文件对话框,如果我在其中选择一个选项,它会显示出来,但我无法打开另一个文件对话框,但是,如果我什么也没选择,程序就会冻结。 如何使它以更常规的方式运行,以使文件对话框仅在请求时始终打开。
from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
class Root(Tk):
def __init__(self):
super(Root, self).__init__()
self.title("Line selector")
self.minsize(800, 720)
self.menu()
def menu(self):
menubar = Menu(self)
self.config(menu = menubar)
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label = 'New', command = self.newfile())
menubar.add_cascade(label="File", menu=filemenu)
def impimg(self):
canvas = Canvas(self, bg = 'white', height = 500, width = 500)
canvas.pack(expand = YES, fill = BOTH)
img = Image.open(str(self.filename))
canvas.image = ImageTk.PhotoImage(img)
canvas.create_image(0, 0, image = canvas.image, anchor = 'nw')
def newfile(self):
self.filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("all files","*.*"),("jpeg files","*.jpg")))
self.impimg()
root = Root()