#๐Ÿ”’ # Need help trying to program a variable and function based on user input

20 messages ยท Page 1 of 1 (latest)

twin orbitBOT
#

@sudden raft

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sudden raft
#
def browsefunc():
    source_dir = tk.filedialog.askdirectory()

b1=tk.Button(root,text='Move From',command=browsefunc)
b1.grid(column=12, row=10)

def browsefunc2():
    end_dir = tk.filedialog.askdirectory()

b2=tk.Button(root,text='Move To',command=browsefunc2)
b2.grid(column=12, row=11)
lilac monolith
#

You look like you're dealing with a scope problem.

#

You'll want a way to have the response be exported out of the function.

#

I'm not very well versed in tkinter, however.

#

Though I imagine a class-based approach would be appropriate enough.

#

Instead of using a function as argument to the command parameters, you could be using a method.

#

That way the method would have access to a self in which state could be stored.

#

As I said, though, I'm not tkintery and that might be the wrong approach.

#

The other option would be use of the global keyword, and I think we should leave that as a last resort.

#
import tkinter as tk

class App:
    def __init__(self):
        self.root = tk.Tk()
        self.end_dir = None
        self.source_dir = None
        self.b1=tk.Button(self.root, text='Move From', command=self.browsefunc)
        self.b1.grid(column=12, row=10)
        self.b2 = tk.Button(self.root, text='Move To',command=self.browsefunc2)
        self.b2.grid(column=12, row=11)

    def browsefunc(self):
        self.source_dir = tk.filedialog.askdirectory()

    def browsefunc2(self):
        self.end_dir = tk.filedialog.askdirectory()

app = App()
app.root.mainloop()
#

Something structured a bit more like this.

sonic hound
#

The OP might be talking about "Variables", which are a way to associate an int or str variable with the value in a widget, which thus edits it.

lilac monolith
#

That...would probably be better.

sonic hound
twin orbitBOT
#

:warning: The owner of this post is no longer in the server.

lilac monolith
#

Well. That settles that.

meager panther
#

!close

twin orbitBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.