tkinter加载文件夹后调用一个函数,使用askdirectory。

huangapple go评论156阅读模式
英文:

tkinter call a function after loading a folder with askdirectory

问题

我正在编写一个简单的图形用户界面,我要求用户选择一个文件夹,如果文件夹被选中,我需要运行一个函数。调用从未进入do_actual_processing函数。我在这里做错了什么?

from tkinter import 
from tkinter import filedialog

def browse_button():
    # 允许用户选择目录并将其存储在名为folder_path的全局变量中
    global folder_path
    folder_name = filedialog.askdirectory()
    if folder_name != '':
        folder_path.config(text=folder_name)
    else:
        folder_path.config(text='No Folder Selected')

window = Tk()

button = Button(window, text="Select Path", command=browse_button)
button.grid(row=0, column=0)

folder_path = Label(window, text='No Folder Selected')
folder_path.grid(row=0, column=1)

folder_path_txt = folder_path.cget("text")
if folder_path_txt != "No Folder Selected":
    id_values = do_actuall_processing(folder_path_txt)

window.mainloop()

注意:代码中有一些拼写错误,例如do_actuall_processing应该是do_actual_processing,并且from tkinter import需要指定具体的导入项,比如from tkinter import Tk, Button, Label等。

英文:

I am writing a simple GUI where I am asking user for a folder and if the folder is selected, I need to run a function. The call never gets inside the do_actual_processing function.
what am I doing wrong here?

from tkinter import 
from tkinter import filedialog

def browse_button():
\# Allow user to select a directory and store it in global var called folder_path
global folder_path
folder_name = filedialog.askdirectory()
if folder_name != '':
folder_path.config(text=folder_name)
else:
folder_path.config(text='No Folder Selected')

window = Tk()

button = Button(window, text="Select Path", command=browse_button)
button.grid(row=0, column=0)

folder_path = Label(window, text='No Folder Selected')
folder_path.grid(row=0, column=1)

folder_path_txt = folder_path.cget("text")
if folder_path_txt != "No Folder Selected"
id_values = do_actuall_processing(folder_path_txt)

window.mainloop()

答案1

得分: 0

以下是要翻译的内容:

存在代码中的多个错误。

第1行:from tkinter import -> 这是语法错误。

第8行:folder_path.config(text=folder_name) -> folder_path 未定义。

第9行:else: -> 没有if语句以开始。

第12行:window = Tk() -> Tk 未导入(与第1行相关)。

第14行:button = Button(window, text="Select Path", command=browse_button) -> Button 未定义。

第17行:folder_path = Label(window, text='No Folder Selected') -> Label 未定义。

第21行:if folder_path_txt != "No Folder Selected" -> 语句没有以冒号结束。

第22行:id_values = do_actuall_processing(folder_path_txt) -> do_actuall_processing 未定义。

一个简单的脚本,可以实现您的请求:

import tkinter as tk
from tkinter import filedialog

def browse_folder():
    folder_path = filedialog.askdirectory()
    if folder_path:
        print(f"Folder selected: {folder_path}")
        # 在此处调用您的函数,传入所选文件夹路径
        # function(folder_path)

root = tk.Tk()

button = tk.Button(root, text="Select Folder", command=browse_folder)
button.pack()

root.mainloop()

当用户单击“Select Folder”按钮时,会弹出文件对话框,允许用户选择文件夹。如果选择了文件夹,browse_folder() 函数将将所选文件夹路径打印到控制台。您可以将print()语句替换为调用您的函数,并将所选文件夹路径作为参数传递。

英文:

There are multiple errors in the code given.

Line 1 : from tkinter import -> This is a syntax error.

Line 8 : folder_path.config(text=folder_name) -> folder_path is not defined.

Line 9 : else: -> There is no if statement to begin with.

Line 12 : window = Tk() -> Tk was never imported (Relate to line 1)

Line 14 : button = Button(window, text="Select Path", command=browse_button) -> Button is not defined.

Line 17 : folder_path = Label(window, text='No Folder Selected') -> Label is not defined.

Line 21 : if folder_path_txt != "No Folder Selected" -> The statement doesnt end with a colon.

Line 22 : id_values = do_actuall_processing(folder_path_txt) ->do_actuall_processing is not defined.

A simple script which does what you are requesting is :

import tkinter as tk
from tkinter import filedialog

def browse_folder():
    folder_path = filedialog.askdirectory()
    if folder_path:
        print(f"Folder selected: {folder_path}")
        # Call your function here with the selected folder path
        # function(folder_path)

root = tk.Tk()

button = tk.Button(root, text="Select Folder", command=browse_folder)
button.pack()

root.mainloop()

When the user clicks the "Select Folder" button, a file dialog will appear allowing the user to select a folder. If a folder is selected, the browse_folder() function will print the selected folder path to the console. You can replace the print() statement with a call to your function, passing in the selected folder path as a parameter.

huangapple
  • 本文由 发表于 2023年4月11日 14:49:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定