【问题标题】:Tkinter - Storing folder directory in function and calling it to change directoryTkinter - 在函数中存储文件夹目录并调用它来更改目录
【发布时间】:2020-07-20 09:22:18
【问题描述】:

我正在尝试使用 tkinter 开发 GUI。我创建了一个函数(作为按钮)来浏览目录文件夹('输入文件夹')。我有一个链接到另一个按钮(“执行”)的例程,该按钮需要“输入文件夹”中的路径。

当我尝试将路径从“输入文件夹”传递到“执行”内的 os.chdir 时,出现错误。示例如下:

import sys
import os
from tkinter import filedialog
from tkinter import *

window = Tk()

def Input():
    filename = filedialog.askdirectory()
    global filename

def Extraction():
    in_loc = filename
    os.chdir(in_loc)

btn = Button(window, text="Extract", bg="black", fg="white", command=Extraction)
btn.pack()

btn2 = Button(text="Input", command=Input).pack()

window.mainloop()

谁能重现这个并告诉我我在这里做错了什么?

谢谢:)

【问题讨论】:

    标签: python tkinter operating-system


    【解决方案1】:

    试试这个:

    import sys
    import os
    from tkinter import filedialog
    from tkinter import *
    
    filename = ''
    
    
    def input_function():
        global filename
    
        filename = filedialog.askdirectory()
    
    
    def extraction():
        global filename
    
        in_loc = filename
        os.chdir(in_loc)
    
    
    window = Tk()
    
    btn = Button(window, text="Extract", bg="black", fg="white", command=extraction)
    btn.pack()
    
    btn2 = Button(text="Input", command=input_function).pack()
    
    window.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2021-09-04
      • 2013-12-04
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 2015-05-09
      • 2021-08-15
      相关资源
      最近更新 更多