By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

GUI text entry tkinter

P: 31
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
Mar 30 '09 #1
Share this Question
Share on Google+
13 Replies


boxfish
Expert 100+
P: 469
I haven't written a Tkinter program for a while, but it looks like you can clear a text entry like this:
Expand|Select|Wrap|Line Numbers
  1. myEntry.delete(0, END)
Is that what you're looking for?
Mar 30 '09 #2

P: 31
for some reason that isnt working for me...

Expand|Select|Wrap|Line Numbers
  1. def evClear():
  2.     q1_data.delete(0,END)
  3.     q2_data.delete(0,END)
  4.     q3_data.delete(0,END)
  5.     q3_data2.delete(0,END)
  6.  
  7.  
  8. def quit(window):
  9.     window.destroy()
  10.  
  11. def run_analyse(db):
  12.     window = Tk()
  13.  
  14.  
  15.     frame = Frame(window)
  16.     frame.pack()
  17.  
  18.     q1_prompt = Label(frame, text="Enter a series:")
  19.     q1_prompt.grid(row=0, column=0)
  20.     q1_data= Entry(frame)
  21.     q1_data.grid(row=0, column=1)
  22.  
  23.     q2_prompt = Label(frame, text="Enter an element:")
  24.     q2_prompt.grid(row=2, column=0)
  25.     q2_data = Entry(frame)
  26.     q2_data.grid(row=2, column=1)
  27.  
  28.     q3_prompt = Label(frame, text="Enter the first element:")
  29.     q3_prompt.grid(row=4, column=0)
  30.     q3_data = Entry(frame)
  31.     q3_data.grid(row=4, column=1)   
  32.     q3_prompt2 = Label(frame, text="Enter the second element:")
  33.     q3_prompt2.grid(row=5, column=0)
  34.     q3_data2 = Entry(frame)
  35.     q3_data2.grid(row=5, column=1)
  36.  
  37.     b1 = Button(frame, text = "Enter")
  38.     b1.grid(row=6, column=1)
  39.  
  40.     quit_button = lambda : quit(window)
  41.     b2 = Button(frame, text = "Quit", command = quit_button)
  42.     b2.grid(row=8, column=1)
  43.  
  44.  
  45.     bClear = Button(frame, text="Clear", command = evClear)
  46.     bClear.grid(row=7, column=1)
  47.  
  48.  
  49.     window.mainloop()
  50.  
  51.  
  52.  
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
Mar 31 '09 #3

boxfish
Expert 100+
P: 469
The text entries are global variables, so evClear is not allowed to access them unless you explicitly say so. Use the global keyword:
Expand|Select|Wrap|Line Numbers
  1. def evClear():
  2.     global q1_data, q2_data, q3_data, q3_data2
  3.     q1_data.delete(0,END)
  4.     q2_data.delete(0,END)
  5.     q3_data.delete(0,END)
  6.     q3_data2.delete(0,END)
  7.  
I hope this helps.
Mar 31 '09 #4

P: 31
@boxfish
ahh thanks boxfish!!
Mar 31 '09 #5

boxfish
Expert 100+
P: 469
You're welcome, glad it worked.
Mar 31 '09 #6

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

bvdet
Expert Mod 2.5K+
P: 2,851
Try making the global declaration inside function run_analyse(db).

-BV
Mar 31 '09 #8

boxfish
Expert 100+
P: 469
@bvdet
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.
Mar 31 '09 #9

bvdet
Expert Mod 2.5K+
P: 2,851
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.
Mar 31 '09 #10

P: 31
@bvdet
how would i do that?..i've never learnt about global before..:s
Mar 31 '09 #11

bvdet
Expert Mod 2.5K+
P: 2,851
Add the following statement inside the body of function run_analyse(db) after the names have been assigned an object.
Expand|Select|Wrap|Line Numbers
  1. global q1_data, q2_data, q3_data, q3_data2
Mar 31 '09 #12

P: 31
hahha thanks bvdet it works perfectly!! btw i had to add it before the names were mentioned.
Mar 31 '09 #13

bvdet
Expert Mod 2.5K+
P: 2,851
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.
Mar 31 '09 #14

Post your reply

Sign in to post your reply or Sign up for a free account.