#๐ # Need help trying to program a variable and function based on user input
20 messages ยท Page 1 of 1 (latest)
@sudden raft
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.
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)
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.
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.
That...would probably be better.
@sudden raft see https://tkdocs.com/shipman/control-variables.html
:warning: The owner of this post is no longer in the server.
Well. That settles that.
!close
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.