簡體   English   中英

函數被方法調用時無法正常工作:Python

[英]Function not working properly when called by a method : Python

因此,當我直接在下面調用列表目錄代碼時,它工作正常。 但是,當我調用main.py時,該應用程序從gui中使用應用程序類,該gui調用了列表目錄功能,它會顯示“ yoo”,但會打印一個空列表,而不是目錄列表。 我被困住了,不知道為什么會這樣。 有任何想法嗎?

輸出:

當列表目錄直接調用時:

["/home/shubham/Desktop/movies/djangounchained.mkv"] 

"yoo"

當main.py用相同的參數調用時:

[]

"yoo"

這是我的主要劇本

from gui import app                                                                                                                            
from list_directory import display_files                                                                                                       
import tkinter as tk                                                                                                                           

root = tk.Tk()                                                                                                                                 
directory = input("Enter directory name:")                                                                                                     
root.geometry("400x300")                                                                                                                       
widgets_creator = app(root)                                                                                                                    
name = "get list"                                                                                                                              
directory_button = widgets_creator.create_button(name,function=display_files,path=directory)                                                   
root.mainloop()     

這是我的GUI腳本

import tkinter as tk                                                                                                                           

class app(tk.Frame):                                                                                                                           
    def __init__(self,master):                                                                                                                 
       super(app,self).__init__(master=master)                                                                                                 
       self.master = master                                                                                                                    
       self.init_window()                                                                                                                      

    def init_window(self):                                                                                                                     

        # changing the title of our master widget                                                                                              
        self.master.title("GUI")                                                                                                               

        # allowing the widget to take the full space of the root window                                                                        
        self.pack(fill=tk.BOTH, expand=1)                                                                                                      

        # creating a button instance                                                                                                           
        quitButton = tk.Button(self, text="Quit")                                                                                              

        # placing the button on my window                                                                                                      
        quitButton.place(x=0, y=0)                                                                                                             


    def create_button(self,button_name,function,path):                                                                                         
        button = tk.Button(self.master,text=button_name,command=lambda: function(path))                                                        
        button.place(x=200,y=5)                                                                                                                
        return button   

這是我的list_directory代碼:

import glob                                                                                                                                    
def display_files(path):                                                                                                                       
    x = glob.glob(path)                                                                                                                        
    print(x)                                                                                                                                   
    print("yoo")                                                                                                                               

if __name__ == '__main__':                                                                                                                     
     display_files("/home/shubham/Desktop/movies/*")                                                                                                                                                                                                                  

我可能已經找到您的問題了。 代碼工作正常,問題出在您的論點上。 例如,如果在出現輸入提示時輸入'/Users/rudy/Desktop/*' ,則結果與您相同。

但是,當我輸入/Users/rudy/Desktop/* (不帶引號)時,一切正常。 input()已經將輸入保存為字符串,因此您無需添加其他引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM