python tkinter update content of a label when a file is opened
up vote
3
down vote
favorite
I'm currently programming a GUI using tkinter and Python 3.
My problem here is i made a Label with which i want to display the path of a file i opened via the askopenfilename() method and this path is not "generated" when i start the program, obviously, so the Label is empty which makes sense but i don't know how to fix it.
I'm gonna put the needed code below (I'm going to cut unnecessary code for this question):
import tkinter as tk
class Graphicaluserinterface(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.grid()
self.fileopenname=tk.StringVar()
self.menubar = tk.Menu(self)
self.create_widgets()
def create_widgets(self):
self.inputpathdisplay = tk.Label(self,textvariable=self.fileopenname,bg="white",width=30)
self.inputpathdisplay.grid(row=1,column=8,columnspan=3,sticky = "W")
def fileopening(self):
from tkinter.filedialog import askopenfilename
self.fileopenname = askopenfilename(filetypes = [("binary files","*.bin*"),("all files","*.*")])
root = tk.Tk()
app = Graphicaluserinterface(master=root)
root.config(menu=app.menubar)
app.mainloop()
I read about using update_idletasks(). If this is correct in my case how would i go about implementing it here?
python-3.x tkinter
add a comment |
up vote
3
down vote
favorite
I'm currently programming a GUI using tkinter and Python 3.
My problem here is i made a Label with which i want to display the path of a file i opened via the askopenfilename() method and this path is not "generated" when i start the program, obviously, so the Label is empty which makes sense but i don't know how to fix it.
I'm gonna put the needed code below (I'm going to cut unnecessary code for this question):
import tkinter as tk
class Graphicaluserinterface(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.grid()
self.fileopenname=tk.StringVar()
self.menubar = tk.Menu(self)
self.create_widgets()
def create_widgets(self):
self.inputpathdisplay = tk.Label(self,textvariable=self.fileopenname,bg="white",width=30)
self.inputpathdisplay.grid(row=1,column=8,columnspan=3,sticky = "W")
def fileopening(self):
from tkinter.filedialog import askopenfilename
self.fileopenname = askopenfilename(filetypes = [("binary files","*.bin*"),("all files","*.*")])
root = tk.Tk()
app = Graphicaluserinterface(master=root)
root.config(menu=app.menubar)
app.mainloop()
I read about using update_idletasks(). If this is correct in my case how would i go about implementing it here?
python-3.x tkinter
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I'm currently programming a GUI using tkinter and Python 3.
My problem here is i made a Label with which i want to display the path of a file i opened via the askopenfilename() method and this path is not "generated" when i start the program, obviously, so the Label is empty which makes sense but i don't know how to fix it.
I'm gonna put the needed code below (I'm going to cut unnecessary code for this question):
import tkinter as tk
class Graphicaluserinterface(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.grid()
self.fileopenname=tk.StringVar()
self.menubar = tk.Menu(self)
self.create_widgets()
def create_widgets(self):
self.inputpathdisplay = tk.Label(self,textvariable=self.fileopenname,bg="white",width=30)
self.inputpathdisplay.grid(row=1,column=8,columnspan=3,sticky = "W")
def fileopening(self):
from tkinter.filedialog import askopenfilename
self.fileopenname = askopenfilename(filetypes = [("binary files","*.bin*"),("all files","*.*")])
root = tk.Tk()
app = Graphicaluserinterface(master=root)
root.config(menu=app.menubar)
app.mainloop()
I read about using update_idletasks(). If this is correct in my case how would i go about implementing it here?
python-3.x tkinter
I'm currently programming a GUI using tkinter and Python 3.
My problem here is i made a Label with which i want to display the path of a file i opened via the askopenfilename() method and this path is not "generated" when i start the program, obviously, so the Label is empty which makes sense but i don't know how to fix it.
I'm gonna put the needed code below (I'm going to cut unnecessary code for this question):
import tkinter as tk
class Graphicaluserinterface(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.grid()
self.fileopenname=tk.StringVar()
self.menubar = tk.Menu(self)
self.create_widgets()
def create_widgets(self):
self.inputpathdisplay = tk.Label(self,textvariable=self.fileopenname,bg="white",width=30)
self.inputpathdisplay.grid(row=1,column=8,columnspan=3,sticky = "W")
def fileopening(self):
from tkinter.filedialog import askopenfilename
self.fileopenname = askopenfilename(filetypes = [("binary files","*.bin*"),("all files","*.*")])
root = tk.Tk()
app = Graphicaluserinterface(master=root)
root.config(menu=app.menubar)
app.mainloop()
I read about using update_idletasks(). If this is correct in my case how would i go about implementing it here?
python-3.x tkinter
python-3.x tkinter
edited Nov 7 at 15:16
Miraj50
1,563517
1,563517
asked Nov 7 at 13:41
James
456
456
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
Right now you are doing self.fileopenname = askopenfilename() and this will redefine self.fileopenname as a string instead of a StringVar(). To correct this you need to set the value of StringVar with set().
That said you should also define all your imports at the top of your code instead of in your function.
import tkinter as tk
from tkinter.filedialog import askopenfilename
class Graphicaluserinterface(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.grid()
self.fileopenname=tk.StringVar()
self.menubar = tk.Menu(self)
self.inputpathdisplay = tk.Label(self, textvariable=self.fileopenname, bg="white")
self.inputpathdisplay.grid(row=1,column=8,columnspan=3,sticky = "W")
self.fileopening()
def fileopening(self):
self.fileopenname.set(askopenfilename(filetypes = [("binary files","*.bin*"),("all files","*.*")]))
root = tk.Tk()
app = Graphicaluserinterface(master=root)
root.config(menu=app.menubar)
app.mainloop()
thanks alot it worked out for me and it changes everytime i select a new file. Can u eloborate on why i don´t need update_idletasks()? I thought when i change something in the program window the mainloop even has to be updated for it to show itself in the window, so why did it work in my case?
– James
Nov 7 at 13:57
1
Themainloop()updates on its own. Tkinter is event driven andmainloop()is like one big loop. So after each event executes it loops over again while updating visuals.
– Mike - SMT
Nov 7 at 13:59
oh ok that cleared things up for me, so i guess i need update_idletasks only for changed that occur before the event is finished
– James
Nov 7 at 14:00
1
@James honestly I have never used it myself. I am sure it has its purpose but its not likely all that necessary.
– Mike - SMT
Nov 7 at 14:03
on another topic, will i be able to use this variable for other methods like using it as a name for a file i create, because print(self.fileopenname) will give me "PY_VAR75"
– James
Nov 7 at 14:07
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Right now you are doing self.fileopenname = askopenfilename() and this will redefine self.fileopenname as a string instead of a StringVar(). To correct this you need to set the value of StringVar with set().
That said you should also define all your imports at the top of your code instead of in your function.
import tkinter as tk
from tkinter.filedialog import askopenfilename
class Graphicaluserinterface(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.grid()
self.fileopenname=tk.StringVar()
self.menubar = tk.Menu(self)
self.inputpathdisplay = tk.Label(self, textvariable=self.fileopenname, bg="white")
self.inputpathdisplay.grid(row=1,column=8,columnspan=3,sticky = "W")
self.fileopening()
def fileopening(self):
self.fileopenname.set(askopenfilename(filetypes = [("binary files","*.bin*"),("all files","*.*")]))
root = tk.Tk()
app = Graphicaluserinterface(master=root)
root.config(menu=app.menubar)
app.mainloop()
thanks alot it worked out for me and it changes everytime i select a new file. Can u eloborate on why i don´t need update_idletasks()? I thought when i change something in the program window the mainloop even has to be updated for it to show itself in the window, so why did it work in my case?
– James
Nov 7 at 13:57
1
Themainloop()updates on its own. Tkinter is event driven andmainloop()is like one big loop. So after each event executes it loops over again while updating visuals.
– Mike - SMT
Nov 7 at 13:59
oh ok that cleared things up for me, so i guess i need update_idletasks only for changed that occur before the event is finished
– James
Nov 7 at 14:00
1
@James honestly I have never used it myself. I am sure it has its purpose but its not likely all that necessary.
– Mike - SMT
Nov 7 at 14:03
on another topic, will i be able to use this variable for other methods like using it as a name for a file i create, because print(self.fileopenname) will give me "PY_VAR75"
– James
Nov 7 at 14:07
|
show 2 more comments
up vote
4
down vote
accepted
Right now you are doing self.fileopenname = askopenfilename() and this will redefine self.fileopenname as a string instead of a StringVar(). To correct this you need to set the value of StringVar with set().
That said you should also define all your imports at the top of your code instead of in your function.
import tkinter as tk
from tkinter.filedialog import askopenfilename
class Graphicaluserinterface(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.grid()
self.fileopenname=tk.StringVar()
self.menubar = tk.Menu(self)
self.inputpathdisplay = tk.Label(self, textvariable=self.fileopenname, bg="white")
self.inputpathdisplay.grid(row=1,column=8,columnspan=3,sticky = "W")
self.fileopening()
def fileopening(self):
self.fileopenname.set(askopenfilename(filetypes = [("binary files","*.bin*"),("all files","*.*")]))
root = tk.Tk()
app = Graphicaluserinterface(master=root)
root.config(menu=app.menubar)
app.mainloop()
thanks alot it worked out for me and it changes everytime i select a new file. Can u eloborate on why i don´t need update_idletasks()? I thought when i change something in the program window the mainloop even has to be updated for it to show itself in the window, so why did it work in my case?
– James
Nov 7 at 13:57
1
Themainloop()updates on its own. Tkinter is event driven andmainloop()is like one big loop. So after each event executes it loops over again while updating visuals.
– Mike - SMT
Nov 7 at 13:59
oh ok that cleared things up for me, so i guess i need update_idletasks only for changed that occur before the event is finished
– James
Nov 7 at 14:00
1
@James honestly I have never used it myself. I am sure it has its purpose but its not likely all that necessary.
– Mike - SMT
Nov 7 at 14:03
on another topic, will i be able to use this variable for other methods like using it as a name for a file i create, because print(self.fileopenname) will give me "PY_VAR75"
– James
Nov 7 at 14:07
|
show 2 more comments
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Right now you are doing self.fileopenname = askopenfilename() and this will redefine self.fileopenname as a string instead of a StringVar(). To correct this you need to set the value of StringVar with set().
That said you should also define all your imports at the top of your code instead of in your function.
import tkinter as tk
from tkinter.filedialog import askopenfilename
class Graphicaluserinterface(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.grid()
self.fileopenname=tk.StringVar()
self.menubar = tk.Menu(self)
self.inputpathdisplay = tk.Label(self, textvariable=self.fileopenname, bg="white")
self.inputpathdisplay.grid(row=1,column=8,columnspan=3,sticky = "W")
self.fileopening()
def fileopening(self):
self.fileopenname.set(askopenfilename(filetypes = [("binary files","*.bin*"),("all files","*.*")]))
root = tk.Tk()
app = Graphicaluserinterface(master=root)
root.config(menu=app.menubar)
app.mainloop()
Right now you are doing self.fileopenname = askopenfilename() and this will redefine self.fileopenname as a string instead of a StringVar(). To correct this you need to set the value of StringVar with set().
That said you should also define all your imports at the top of your code instead of in your function.
import tkinter as tk
from tkinter.filedialog import askopenfilename
class Graphicaluserinterface(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.grid()
self.fileopenname=tk.StringVar()
self.menubar = tk.Menu(self)
self.inputpathdisplay = tk.Label(self, textvariable=self.fileopenname, bg="white")
self.inputpathdisplay.grid(row=1,column=8,columnspan=3,sticky = "W")
self.fileopening()
def fileopening(self):
self.fileopenname.set(askopenfilename(filetypes = [("binary files","*.bin*"),("all files","*.*")]))
root = tk.Tk()
app = Graphicaluserinterface(master=root)
root.config(menu=app.menubar)
app.mainloop()
answered Nov 7 at 13:52
Mike - SMT
8,4932934
8,4932934
thanks alot it worked out for me and it changes everytime i select a new file. Can u eloborate on why i don´t need update_idletasks()? I thought when i change something in the program window the mainloop even has to be updated for it to show itself in the window, so why did it work in my case?
– James
Nov 7 at 13:57
1
Themainloop()updates on its own. Tkinter is event driven andmainloop()is like one big loop. So after each event executes it loops over again while updating visuals.
– Mike - SMT
Nov 7 at 13:59
oh ok that cleared things up for me, so i guess i need update_idletasks only for changed that occur before the event is finished
– James
Nov 7 at 14:00
1
@James honestly I have never used it myself. I am sure it has its purpose but its not likely all that necessary.
– Mike - SMT
Nov 7 at 14:03
on another topic, will i be able to use this variable for other methods like using it as a name for a file i create, because print(self.fileopenname) will give me "PY_VAR75"
– James
Nov 7 at 14:07
|
show 2 more comments
thanks alot it worked out for me and it changes everytime i select a new file. Can u eloborate on why i don´t need update_idletasks()? I thought when i change something in the program window the mainloop even has to be updated for it to show itself in the window, so why did it work in my case?
– James
Nov 7 at 13:57
1
Themainloop()updates on its own. Tkinter is event driven andmainloop()is like one big loop. So after each event executes it loops over again while updating visuals.
– Mike - SMT
Nov 7 at 13:59
oh ok that cleared things up for me, so i guess i need update_idletasks only for changed that occur before the event is finished
– James
Nov 7 at 14:00
1
@James honestly I have never used it myself. I am sure it has its purpose but its not likely all that necessary.
– Mike - SMT
Nov 7 at 14:03
on another topic, will i be able to use this variable for other methods like using it as a name for a file i create, because print(self.fileopenname) will give me "PY_VAR75"
– James
Nov 7 at 14:07
thanks alot it worked out for me and it changes everytime i select a new file. Can u eloborate on why i don´t need update_idletasks()? I thought when i change something in the program window the mainloop even has to be updated for it to show itself in the window, so why did it work in my case?
– James
Nov 7 at 13:57
thanks alot it worked out for me and it changes everytime i select a new file. Can u eloborate on why i don´t need update_idletasks()? I thought when i change something in the program window the mainloop even has to be updated for it to show itself in the window, so why did it work in my case?
– James
Nov 7 at 13:57
1
1
The
mainloop() updates on its own. Tkinter is event driven and mainloop() is like one big loop. So after each event executes it loops over again while updating visuals.– Mike - SMT
Nov 7 at 13:59
The
mainloop() updates on its own. Tkinter is event driven and mainloop() is like one big loop. So after each event executes it loops over again while updating visuals.– Mike - SMT
Nov 7 at 13:59
oh ok that cleared things up for me, so i guess i need update_idletasks only for changed that occur before the event is finished
– James
Nov 7 at 14:00
oh ok that cleared things up for me, so i guess i need update_idletasks only for changed that occur before the event is finished
– James
Nov 7 at 14:00
1
1
@James honestly I have never used it myself. I am sure it has its purpose but its not likely all that necessary.
– Mike - SMT
Nov 7 at 14:03
@James honestly I have never used it myself. I am sure it has its purpose but its not likely all that necessary.
– Mike - SMT
Nov 7 at 14:03
on another topic, will i be able to use this variable for other methods like using it as a name for a file i create, because print(self.fileopenname) will give me "PY_VAR75"
– James
Nov 7 at 14:07
on another topic, will i be able to use this variable for other methods like using it as a name for a file i create, because print(self.fileopenname) will give me "PY_VAR75"
– James
Nov 7 at 14:07
|
show 2 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190634%2fpython-tkinter-update-content-of-a-label-when-a-file-is-opened%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown