无法使 folder_path = filedialog.askdirectory() 全局化
Can't make folder_path = filedialog.askdirectory() global
我不知道为什么,但即使我在函数中将变量 folder_path 声明为全局变量,如果我在它外面打印它,值也不会改变....
这是代码
import cv2
import pytesseract
import os
import tkinter as tk
from tkinter import filedialog
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'
class images():
def __init__(self,path):
self.path = path
self.cv = cv2.imread(self.path)
folder_path = 'if u see this after selecting a folder the global isnt working'
def browseButton():
global folder_path
folder_path = filedialog.askdirectory()
def newfunc():
print(folder_path)
#-----------------------------------GUI---------------------------------------------------
window = tk.Tk()
window.geometry('600x400')
window.title('Text scanner')
window.resizable(False,False)
bg = tk.Canvas(window, bg="#ebba34", width='590', height='390')
intro_label = tk.Label(window,
text="Benvenuto nel Text Scanner, seleziona il percorso file dove hai inserito le immagini.",
font=("Helvetica", 9))
file_path_entry = tk.Entry()
file_path_button = tk.Button(cursor='hand2', text='Browse', command=browseButton)
bg.pack()
intro_label.place(x=68,y=20)
file_path_entry.place(x=17,y=50, width=513)
file_path_button.place(x=533,y=50,height=20, width=50)
#-----------------------------------MAIN LOOP-----------------------------------------
if __name__ == '__main__':
window.mainloop()
newfunc()
现在我在关闭程序之前没有得到任何输出
但是,如果我将 print 放入函数中,它会正确输出 folder_path 但我需要在函数外部将其用于另一个函数(我无法将其与这个函数结合使用)
只要 browseButton()
未执行,您的 folder_path
就不会改变,因此将其保留在代码的主块中不会更改路径,相反,您应该将其保留在像您这样的函数中推测。
However if I put the print inside the function it outputs the folder_path correctly but I need it outside of it to use it into another function (which I cannot combine with this one)
这在任何情况下都不需要,您可以在任何地方创建一个新函数并在其中使用 folder_path
。只要browseButton()
被执行,folder_path
就会被赋予新的值。
def new_func():
print(folder_path) # New path will be printed
换句话说,您在将 folder_path
定义为 'if u see this after selecting a folder the global isnt working' 之后立即打印,更改其定义的函数没有机会执行或更改变量的值,因此,您应该将 print(folder_path)
移动到仅在 browseButton()
执行后运行的其他函数中。
我不知道为什么,但即使我在函数中将变量 folder_path 声明为全局变量,如果我在它外面打印它,值也不会改变....
这是代码
import cv2
import pytesseract
import os
import tkinter as tk
from tkinter import filedialog
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe'
class images():
def __init__(self,path):
self.path = path
self.cv = cv2.imread(self.path)
folder_path = 'if u see this after selecting a folder the global isnt working'
def browseButton():
global folder_path
folder_path = filedialog.askdirectory()
def newfunc():
print(folder_path)
#-----------------------------------GUI---------------------------------------------------
window = tk.Tk()
window.geometry('600x400')
window.title('Text scanner')
window.resizable(False,False)
bg = tk.Canvas(window, bg="#ebba34", width='590', height='390')
intro_label = tk.Label(window,
text="Benvenuto nel Text Scanner, seleziona il percorso file dove hai inserito le immagini.",
font=("Helvetica", 9))
file_path_entry = tk.Entry()
file_path_button = tk.Button(cursor='hand2', text='Browse', command=browseButton)
bg.pack()
intro_label.place(x=68,y=20)
file_path_entry.place(x=17,y=50, width=513)
file_path_button.place(x=533,y=50,height=20, width=50)
#-----------------------------------MAIN LOOP-----------------------------------------
if __name__ == '__main__':
window.mainloop()
newfunc()
现在我在关闭程序之前没有得到任何输出
但是,如果我将 print 放入函数中,它会正确输出 folder_path 但我需要在函数外部将其用于另一个函数(我无法将其与这个函数结合使用)
只要 browseButton()
未执行,您的 folder_path
就不会改变,因此将其保留在代码的主块中不会更改路径,相反,您应该将其保留在像您这样的函数中推测。
However if I put the print inside the function it outputs the folder_path correctly but I need it outside of it to use it into another function (which I cannot combine with this one)
这在任何情况下都不需要,您可以在任何地方创建一个新函数并在其中使用 folder_path
。只要browseButton()
被执行,folder_path
就会被赋予新的值。
def new_func():
print(folder_path) # New path will be printed
换句话说,您在将 folder_path
定义为 'if u see this after selecting a folder the global isnt working' 之后立即打印,更改其定义的函数没有机会执行或更改变量的值,因此,您应该将 print(folder_path)
移动到仅在 browseButton()
执行后运行的其他函数中。