【问题标题】:How to use a file that was selected through file dialog?如何使用通过文件对话框选择的文件?
【发布时间】:2017-10-05 00:12:10
【问题描述】:

我的问题是如何使用稍后我将通过 askopenfilename() 选择的文件,例如将其放在画布上? 我应该用什么代替“?”在“我 = ?” ? 谢谢 ! 对不起,我是个初学者

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()

【问题讨论】:

    标签: python canvas tkinter filedialog


    【解决方案1】:

    使用此代码将文件存储为变量:

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

    模式可以是:

    'r' - 如果你想从文件中读取数据。

    'w' - 如果你想将数据写入文件。

    您可以使用以下命令读取文件的数据:file.read()。 并使用命令写入数据:file.write(data)(模式应相应)。

    here 中进一步阅读。

    希望这会对你有所帮助,Yahli。

    【讨论】:

    • 非常感谢您的回答,但我真的不明白:我应该把这个放在我的代码中的什么地方?我真的是一个初学者所以.. :)
    猜你喜欢
    • 2013-04-19
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2023-03-29
    • 2017-11-30
    • 1970-01-01
    相关资源
    最近更新 更多