GUI text entry tkinter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • v13tn1g
    New Member
    • Feb 2009
    • 31

    GUI text entry tkinter

    so basically my GUI window has 4 text entries where the user inputs stuff. i have created a clear button with in my window, the thing that i was wondering is how do i create a function to associate with the clear button to clear all text entries entered by the user?.

    thanks
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    I haven't written a Tkinter program for a while, but it looks like you can clear a text entry like this:
    Code:
    myEntry.delete(0, END)
    Is that what you're looking for?

    Comment

    • v13tn1g
      New Member
      • Feb 2009
      • 31

      #3
      for some reason that isnt working for me...

      Code:
      def evClear():
          q1_data.delete(0,END)
          q2_data.delete(0,END)
          q3_data.delete(0,END)
          q3_data2.delete(0,END)
          
      
      def quit(window):
          window.destroy()
      
      def run_analyse(db):
          window = Tk()
          
         
          frame = Frame(window)
          frame.pack()
          
          q1_prompt = Label(frame, text="Enter a series:")
          q1_prompt.grid(row=0, column=0)
          q1_data= Entry(frame)
          q1_data.grid(row=0, column=1)
           
          q2_prompt = Label(frame, text="Enter an element:")
          q2_prompt.grid(row=2, column=0)
          q2_data = Entry(frame)
          q2_data.grid(row=2, column=1)
          
          q3_prompt = Label(frame, text="Enter the first element:")
          q3_prompt.grid(row=4, column=0)
          q3_data = Entry(frame)
          q3_data.grid(row=4, column=1)   
          q3_prompt2 = Label(frame, text="Enter the second element:")
          q3_prompt2.grid(row=5, column=0)
          q3_data2 = Entry(frame)
          q3_data2.grid(row=5, column=1)
          
          b1 = Button(frame, text = "Enter")
          b1.grid(row=6, column=1)
          
          quit_button = lambda : quit(window)
          b2 = Button(frame, text = "Quit", command = quit_button)
          b2.grid(row=8, column=1)
          
          
          bClear = Button(frame, text="Clear", command = evClear)
          bClear.grid(row=7, column=1)
              
          
          window.mainloop()
      for some reason it doesnt clear the entries....it says that they are not defined...such as q1_data is not defined...but it is :s can you debug this?

      thanks

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        The text entries are global variables, so evClear is not allowed to access them unless you explicitly say so. Use the global keyword:
        Code:
        def evClear():
            global q1_data, q2_data, q3_data, q3_data2
            q1_data.delete(0,END)
            q2_data.delete(0,END)
            q3_data.delete(0,END)
            q3_data2.delete(0,END)
        I hope this helps.

        Comment

        • v13tn1g
          New Member
          • Feb 2009
          • 31

          #5
          Originally posted by boxfish
          The text entries are global variables, so evClear is not allowed to access them unless you explicitly say so. Use the global keyword:
          Code:
          def evClear():
              global q1_data, q2_data, q3_data, q3_data2
              q1_data.delete(0,END)
              q2_data.delete(0,END)
              q3_data.delete(0,END)
              q3_data2.delete(0,END)
          I hope this helps.
          ahh thanks boxfish!!

          Comment

          • boxfish
            Recognized Expert Contributor
            • Mar 2008
            • 469

            #6
            You're welcome, glad it worked.

            Comment

            • v13tn1g
              New Member
              • Feb 2009
              • 31

              #7
              hmm acutally inputting that code didn't work :s, it still says q1_data is not defined

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Try making the global declaration inside function run_analyse(db).

                -BV

                Comment

                • boxfish
                  Recognized Expert Contributor
                  • Mar 2008
                  • 469

                  #9
                  Originally posted by bvdet
                  Try making the global declaration inside function run_analyse(db).

                  -BV
                  Wow, that works! That really baffles me. What is the global statement doing? Is it actually turning them into global variables? I thought the global statement only made variables available for the function that used it. I definitely learned something today.

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    When Python resolves an identifier, it first checks the local namespace, then checks the global namespace, and then checks the __builtins__ namespace before raising a NameError exception. The global namespace for a function is always the module in which the function is defined.

                    Comment

                    • v13tn1g
                      New Member
                      • Feb 2009
                      • 31

                      #11
                      Originally posted by bvdet
                      Try making the global declaration inside function run_analyse(db).

                      -BV
                      how would i do that?..i've never learnt about global before..:s

                      Comment

                      • bvdet
                        Recognized Expert Specialist
                        • Oct 2006
                        • 2851

                        #12
                        Add the following statement inside the body of function run_analyse(db) after the names have been assigned an object.
                        Code:
                        global q1_data, q2_data, q3_data, q3_data2

                        Comment

                        • v13tn1g
                          New Member
                          • Feb 2009
                          • 31

                          #13
                          hahha thanks bvdet it works perfectly!! btw i had to add it before the names were mentioned.

                          Comment

                          • bvdet
                            Recognized Expert Specialist
                            • Oct 2006
                            • 2851

                            #14
                            v13tn1g,

                            I am pleased it works for you. Yes, the global declaration must be made before the assignment. I have not used global in years, and I forgot the proper syntax.

                            Comment

                            Working...