0

I'm trying to open a user selected file (that contains orders). At the command of a button.

Therefore I have a "select file" function attached to a "Browse" button which works perfectly and stores the path in selected_orders.

After selecting the file the user should click a "search" button attached to a function that (for now) opens the file and prints it contents. This search button does not work. The function however does get called on run where I get an error (filenotfounderror [errorno2]). I have "solved" the error with an if/else block but when pushing the button it does not call the search function, and I cant figure out why.

I don't get an error with the if/else block, its just I can push that button till I'm old and grey, and still nothing will have happened. when stepping trough the program line by line, pushing the button does also net select the code in de search function.

can anybody see and explain why my search button does not print the contents of the file saved in selected_orders? and how to fix it?

from tkinter import *
from tkinter.filedialog import askopenfilename

Gui = Tk()
Gui.geometry("800x600")

selected_orders = ""

def select_file():
    selected_orders = askopenfilename()
    select_orders.configure(text = selected_orders)
   

select_orders = Button(Gui, text = "Browse", command = select_file)
select_orders.pack()

def searchfunc():
    
    if selected_orders == "":
        print ("no orders selected yet")

    else:
        with open(selected_orders) as order_data:
            orders = order_data.readlines()
            print(orders)


search = Button(Gui, text = "Search", command = searchfunc())
search.pack()

Gui.mainloop()

a bit of a disclaimer but this is my first post so if my question is not up to standards I'm sorry, please let me know how to improve. I also taught myself python, so if my code is not completely zen again, I'm sorry, please let me know how to improve.

Axel
  • 1
  • 1
    `command = searchfunc()` should be `command = searchfunc`. – tevemadar Sep 26 '21 at 16:27
  • 1
    You should just supply the name of the function like this: `command = searchfunc`. What you actually did was to call the function, which then returned `None` and when `Button` sees `None` as the command it does nothing. – quamrana Sep 26 '21 at 16:27
  • 1
    In the button, change the function from ```searchfunc()``` to ```searchfunc```. No need of the parenthesis. – IJ_123 Sep 27 '21 at 06:42

0 Answers0