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?










share|improve this question




























    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?










    share|improve this question


























      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 15:16









      Miraj50

      1,563517




      1,563517










      asked Nov 7 at 13:41









      James

      456




      456
























          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()





          share|improve this answer





















          • 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




            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








          • 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













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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()





          share|improve this answer





















          • 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




            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








          • 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

















          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()





          share|improve this answer





















          • 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




            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








          • 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















          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()





          share|improve this answer












          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()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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




            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








          • 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






          • 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










          • 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




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          這個網誌中的熱門文章

          Academy of Television Arts & Sciences

          L'Équipe

          1995 France bombings