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

change a variable thru a dialog box

P: 2
How do I change a variable thru an entry dialog box in tkinter or python, as x = 1 and the user changes it to x = 2?? I can't seem to find the answer.
Sep 24 '11 #1

✓ answered by bvdet

Assign textvariable to the IntVar.

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. master = Tk()
  4.  
  5. master.title("test")
  6.  
  7. w = Canvas(master, width=500, height=500)
  8. w.pack()
  9.  
  10. w.config(background="black")
  11.  
  12. defv = 100
  13. a = IntVar()
  14. a.set(defv)
  15. v2 = a.get()
  16.  
  17. e = Entry(master, textvariable=a)
  18. e.pack()
  19.  
  20. def callback():
  21.     v2 = a.get()
  22.     w.create_text(100, 100, font="Purisa", text=v2, fill="white")
  23.  
  24. c = Button(master, text="change", width=10, command=callback)
  25. c.pack()
  26.  
  27. master.mainloop()

Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Jay,

Could you post the code you tried? Have you created a textvariable?
Sep 25 '11 #2

P: 2
Hi, thanks for your reply. This is the code I've been trying with. I can write a variable on the window but I haven't been able to start with a default value then change it through the entry widget. Please help it is driving me crazy.

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. master = Tk()
  4.  
  5. master.title("test")
  6.  
  7. w = Canvas(master, width=500, height=500)
  8. w.pack()
  9.  
  10. w.config(background="black")
  11.  
  12. defv = 100
  13. a = IntVar()
  14. a.set(defv)
  15. v2 = a.get()
  16.  
  17. e = Entry(master)
  18. e.pack()
  19.  
  20. def callback():
  21.     v = e.get()
  22.     a.set(v)
  23.     v2 = a.get()
  24.     w.create_text(100, 100, font="Purisa", text=v2, fill="white")
  25.  
  26. c = Button(master, text="change", width=10, command=callback)
  27. c.pack()
  28.  
  29. master.mainloop()
  30.  
Sep 25 '11 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Assign textvariable to the IntVar.

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. master = Tk()
  4.  
  5. master.title("test")
  6.  
  7. w = Canvas(master, width=500, height=500)
  8. w.pack()
  9.  
  10. w.config(background="black")
  11.  
  12. defv = 100
  13. a = IntVar()
  14. a.set(defv)
  15. v2 = a.get()
  16.  
  17. e = Entry(master, textvariable=a)
  18. e.pack()
  19.  
  20. def callback():
  21.     v2 = a.get()
  22.     w.create_text(100, 100, font="Purisa", text=v2, fill="white")
  23.  
  24. c = Button(master, text="change", width=10, command=callback)
  25. c.pack()
  26.  
  27. master.mainloop()
Sep 25 '11 #4

Post your reply

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