我是使用Tkinter软件包的新手,我正为一个问题而苦苦挣扎。
我正在尝试通过tkFileDialog
导入图像,
然后显示导入的图像
使用PIL.Image
在单独的框架上,但是我的代码无法正常工作。
我做到了:
img_name = StringVar()
def directory():
'''Function that activates when pressing button'''
'''Uses tkFileDialog to bring in an image (.jpg, .png)'''
img_name.set(filename) # set full path of the image.
img_print.set(only_filename[::-1]) # set only the name of the image.
然后在定义之外,我尝试了一下,但没有打印任何内容。
img_name.get()
必须工作,这样我才能做
Image.open(img_name)
import Tkinter as tk
import ttk
from Tkinter import *
import tkMessageBox
import tkFileDialog
import tkFont
from PIL import Image, ImageTk
## --------------------- DEFINITIONS ------------------------------##
# Import local file
def directory():
filename = tkFileDialog.askopenfilename(
initialdir='~/Desktop/Bachelor_Project/GUI_Tkinetr')
img_name = img_name.get()
img_print = img_print.get()
only_filename = ''
for letter in filename[::-1]:
if letter == '/':
break
only_filename += letter
img_name.set(filename) # Full pathway
img_print.set(only_filename[::-1]) # Only the image name
img = Image.open(filename)
img_size.set(str(img.size[0]) + "x" + str(img.size[1]))
#---------------------- INITIALIZATION / INPUTS ------------------------------------#
parent = Tk()
parent.title('Shoulder pointer')
parent.geometry('900x450')
img_name = StringVar() # [instance]full image pathway
img_print = StringVar() # [instance] Only the image name
img_size = StringVar() # [instance] Image size ex) 512x512
mainframe = Frame(parent)
## Minor frame 1 construction ##
# Display imported image
''' Getting Errors, I don't know how to solve.'''
imported_img = Image.open(img_name.get())
img_integrate = ImageTk.PhotoImage(imported_img)
ttk.Label(mainframe, image=img_integrate)
parent.mainloop()