2017-05-06 87 views
1

我的問題是如何使用我將在稍後用askopenfilename()選擇的文件,例如將它放在畫布上? 我應該放什麼而不是「?」在「Im =?」 ? 謝謝! 對不起我非常初學者如何使用通過文件對話框選擇的文件?

import tkinter as tk 
from tkinter import * 
from tkinter.filedialog import * 



root=tk.Tk() 
root.geometry('1000x690') 
root.title("Baccalauréat ISN 2017") 

# # # 
def Open_Image(): 
askopenfilename() 

# # # 
B13= Button(root, text='Open Image', height=5, width= 25, command = askopenfilename) 
B13.grid(row=1, column=5, sticky= W + E) 

Im = ? 
# # # 




Nim = Im.resize((int((Im.width*514)/Im.height), 514)) #maxsize =(821, 514) ---> size of the canvas 821-length; 514 -height 


nshow = ImageTk.PhotoImage(Nim) 

Can = tk.Canvas(root, background = 'blue') 

Can.grid(row = 1, column = 0, rowspan = 6, columnspan = 5, sticky = W + E + N + S) 
Cim = Can.create_image(0, 0, anchor = NW, image = nshow) # "0, 0" space between the picture and the borders 

# # # 

mainloop() 

回答

0

使用此代碼來存儲文件爲一個變量:

path = tkFileDialog.askdirectory() 
    os.chdir(path) 
    f = open(file_name, mode) 

模式可以是:

「R」 - 如果你想從文件讀取數據。

'w' - 如果要將數據寫入文件。

您可以使用以下命令讀取文件的數據:file.read()。 並使用命令寫入數據:file.write(data)(模式應相應)。

進一步閱讀here

希望這會幫助你,Yahli。

+0

非常感謝你的回答,但是 我真的不明白它:我應該在哪裏從我的代碼中解決這個問題? 我真的是一個初學者,所以.. :) –

相關問題