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

how to update Label "e1" ?

P: 5
I wrote a code and wanna update the PATHNAME for E1.
The Print show's that the Pathname is updating,
but the LabelField is not updating.
What i am doing wrong ?


Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/python
  3. # -*- coding: utf-8 -*-
  4. # http://zetcode.com/gui/tkinter/layout/
  5. # http://docs.activestate.com/komodo/3.5/komodo-doc-guibuilder.html
  6. # http://sebsauvage.net/python/gui/
  7.  
  8.  
  9. from Tkinter import *
  10. from tkMessageBox import *
  11. from tkFileDialog import *
  12. from ttk import *
  13.  
  14. global pathname
  15. pathname ="pathnamevariabl"
  16.  
  17. import os
  18.  
  19. def answer():
  20.     showerror("Answer", "Sorry, no answer available")
  21.  
  22. def CloseButtonClass():
  23.         quit()
  24.  
  25. def BrowseButtonClass():
  26.     name = askopenfilename()
  27.  
  28.     global pathname
  29.  
  30.     pathname = os.path.split(name)[0]
  31.  
  32.     print pathname
  33.  
  34.  
  35. #    versuch nach dem browser das script zu starten mit dem parameter des folders
  36. #    os.system('upload_with_authentication.py pathname')
  37.  
  38.  
  39. def HelpButtonClass():
  40.     showwarning('Help', 'Not yet implemented')
  41.  
  42. def GoButtonClass():
  43.     showwarning('Go', 'Not yet implemented')
  44.  
  45. class MapillaryGui(Frame):
  46.  
  47.     def __init__(self, parent):
  48.         Frame.__init__(self, parent)   
  49.  
  50.         self.parent = parent
  51.  
  52.         self.initUI()
  53.  
  54.     def initUI(self):
  55.  
  56.         self.parent.title("Mapillary Upload GUI")
  57.         self.style = Style()
  58.         self.style.theme_use("default")
  59.         self.pack(fill=BOTH, expand=1)
  60.  
  61.         self.columnconfigure(1, weight=1)
  62.         self.columnconfigure(3, pad=7)
  63.         self.rowconfigure(3, weight=1)
  64.         self.rowconfigure(5, pad=7)
  65.  
  66.         lbl = Label(self, text="Folder")
  67.         lbl.grid(sticky=W, pady=4, padx=5)
  68.  
  69.  
  70.         #das weisse Feld  - old code
  71.         #area = Text(self)
  72.  
  73.         area = Text(self)
  74.  
  75.         Label(area, text="Threat 1 : ").grid(row=0)
  76.         Label(area, text="Threat 2 : ").grid(row=1)
  77.  
  78.         e1 = Entry(area)
  79.         e2 = Entry(area)
  80.  
  81.         e1.grid(row=0, column=1)
  82.         e2.grid(row=1, column=1)
  83.  
  84.         e1.insert(10,pathname)
  85.  
  86.         area.grid(row=1, column=0, columnspan=2, rowspan=4, 
  87.             padx=5, sticky=E+W+S+N)
  88.  
  89.  
  90.         # Button Browse
  91.         abtn = Button(self, text="Browse", command=BrowseButtonClass)
  92.         abtn.grid(row=1, column=3)
  93.  
  94.         #Button Close
  95.         cbtn = Button(self, text="Close", command=CloseButtonClass)
  96.         cbtn.grid(row=2, column=3, pady=4)
  97.  
  98.         #Button Help
  99.         hbtn = Button(self, text="Help", command=HelpButtonClass)
  100.         hbtn.grid(row=5, column=0, padx=5)
  101.  
  102.         #Button Go
  103.         obtn = Button(self, text="GO", command=GoButtonClass)
  104.         obtn.grid(row=5, column=3)
  105.  
  106.  
  107. def main():
  108.  
  109.     root = Tk()
  110.     root.geometry("300x200+300+300")
  111.     app = MapillaryGui(root)
  112.     root.mainloop()
  113.  
  114.  
  115. if __name__ == '__main__':
  116.     main()
  117.  
Jul 16 '15 #1

✓ answered by bvdet

One way is to create a StringVar and assign it to the entry field textvariable option. Use the StringVar object's method set to assign a value. Use a lambda in the browse button command assignment so you can pass the StringVar as an argument for reassignment. You should not need a global variable.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/python
  3. # -*- coding: utf-8 -*-
  4. # http://zetcode.com/gui/tkinter/layout/
  5. # http://docs.activestate.com/komodo/3.5/komodo-doc-guibuilder.html
  6. # http://sebsauvage.net/python/gui/
  7.  
  8. from Tkinter import *
  9. from tkMessageBox import *
  10. from tkFileDialog import *
  11. from ttk import *
  12.  
  13. global pathname
  14. pathname ="pathnamevariabl"
  15.  
  16. import os
  17.  
  18. def answer():
  19.     showerror("Answer", "Sorry, no answer available")
  20.  
  21. def CloseButtonClass():
  22.         quit()
  23.  
  24. def BrowseButtonClass(var):
  25.     name = askopenfilename()
  26.     global pathname
  27.     pathname = os.path.split(name)[0]
  28.     print pathname
  29.     var.set(pathname)
  30.  
  31.  
  32. #    versuch nach dem browser das script zu starten mit dem parameter des folders
  33. #    os.system('upload_with_authentication.py pathname')
  34.  
  35.  
  36. def HelpButtonClass():
  37.     showwarning('Help', 'Not yet implemented')
  38.  
  39. def GoButtonClass():
  40.     showwarning('Go', 'Not yet implemented')
  41.  
  42. class MapillaryGui(Frame):
  43.  
  44.     def __init__(self, parent):
  45.         Frame.__init__(self, parent)   
  46.  
  47.         self.parent = parent
  48.  
  49.         self.initUI()
  50.  
  51.     def initUI(self):
  52.  
  53.         self.parent.title("Mapillary Upload GUI")
  54.         self.style = Style()
  55.         self.style.theme_use("default")
  56.         self.pack(fill=BOTH, expand=1)
  57.  
  58.         self.columnconfigure(1, weight=1)
  59.         self.columnconfigure(3, pad=7)
  60.         self.rowconfigure(3, weight=1)
  61.         self.rowconfigure(5, pad=7)
  62.  
  63.         lbl = Label(self, text="Folder")
  64.         lbl.grid(sticky=W, pady=4, padx=5)
  65.  
  66.  
  67.         #das weisse Feld  - old code
  68.         #area = Text(self)
  69.  
  70.         area = Text(self)
  71.  
  72.         Label(area, text="Threat 1 : ").grid(row=0)
  73.         Label(area, text="Threat 2 : ").grid(row=1)
  74.  
  75.         value = StringVar()
  76.         value.set(pathname)
  77.         e1 = Entry(area, textvariable=value)
  78.         e2 = Entry(area)
  79.  
  80.         e1.grid(row=0, column=1)
  81.         e2.grid(row=1, column=1)
  82.  
  83.         e1.insert(10,pathname)
  84.  
  85.         area.grid(row=1, column=0, columnspan=2, rowspan=4, 
  86.             padx=5, sticky=E+W+S+N)
  87.  
  88.         # Button Browse
  89.         abtn = Button(self, text="Browse", command=lambda:BrowseButtonClass(value))
  90.         abtn.grid(row=1, column=3)
  91.  
  92.         #Button Close
  93.         cbtn = Button(self, text="Close", command=CloseButtonClass)
  94.         cbtn.grid(row=2, column=3, pady=4)
  95.  
  96.         #Button Help
  97.         hbtn = Button(self, text="Help", command=HelpButtonClass)
  98.         hbtn.grid(row=5, column=0, padx=5)
  99.  
  100.         #Button Go
  101.         obtn = Button(self, text="GO", command=GoButtonClass)
  102.         obtn.grid(row=5, column=3)
  103.  
  104.  
  105. def main():
  106.  
  107.     root = Tk()
  108.     root.geometry("300x200+300+300")
  109.     app = MapillaryGui(root)
  110.     root.mainloop()
  111.  
  112.  
  113. if __name__ == '__main__':
  114.     main()
  115.  

Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
One way is to create a StringVar and assign it to the entry field textvariable option. Use the StringVar object's method set to assign a value. Use a lambda in the browse button command assignment so you can pass the StringVar as an argument for reassignment. You should not need a global variable.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/python
  3. # -*- coding: utf-8 -*-
  4. # http://zetcode.com/gui/tkinter/layout/
  5. # http://docs.activestate.com/komodo/3.5/komodo-doc-guibuilder.html
  6. # http://sebsauvage.net/python/gui/
  7.  
  8. from Tkinter import *
  9. from tkMessageBox import *
  10. from tkFileDialog import *
  11. from ttk import *
  12.  
  13. global pathname
  14. pathname ="pathnamevariabl"
  15.  
  16. import os
  17.  
  18. def answer():
  19.     showerror("Answer", "Sorry, no answer available")
  20.  
  21. def CloseButtonClass():
  22.         quit()
  23.  
  24. def BrowseButtonClass(var):
  25.     name = askopenfilename()
  26.     global pathname
  27.     pathname = os.path.split(name)[0]
  28.     print pathname
  29.     var.set(pathname)
  30.  
  31.  
  32. #    versuch nach dem browser das script zu starten mit dem parameter des folders
  33. #    os.system('upload_with_authentication.py pathname')
  34.  
  35.  
  36. def HelpButtonClass():
  37.     showwarning('Help', 'Not yet implemented')
  38.  
  39. def GoButtonClass():
  40.     showwarning('Go', 'Not yet implemented')
  41.  
  42. class MapillaryGui(Frame):
  43.  
  44.     def __init__(self, parent):
  45.         Frame.__init__(self, parent)   
  46.  
  47.         self.parent = parent
  48.  
  49.         self.initUI()
  50.  
  51.     def initUI(self):
  52.  
  53.         self.parent.title("Mapillary Upload GUI")
  54.         self.style = Style()
  55.         self.style.theme_use("default")
  56.         self.pack(fill=BOTH, expand=1)
  57.  
  58.         self.columnconfigure(1, weight=1)
  59.         self.columnconfigure(3, pad=7)
  60.         self.rowconfigure(3, weight=1)
  61.         self.rowconfigure(5, pad=7)
  62.  
  63.         lbl = Label(self, text="Folder")
  64.         lbl.grid(sticky=W, pady=4, padx=5)
  65.  
  66.  
  67.         #das weisse Feld  - old code
  68.         #area = Text(self)
  69.  
  70.         area = Text(self)
  71.  
  72.         Label(area, text="Threat 1 : ").grid(row=0)
  73.         Label(area, text="Threat 2 : ").grid(row=1)
  74.  
  75.         value = StringVar()
  76.         value.set(pathname)
  77.         e1 = Entry(area, textvariable=value)
  78.         e2 = Entry(area)
  79.  
  80.         e1.grid(row=0, column=1)
  81.         e2.grid(row=1, column=1)
  82.  
  83.         e1.insert(10,pathname)
  84.  
  85.         area.grid(row=1, column=0, columnspan=2, rowspan=4, 
  86.             padx=5, sticky=E+W+S+N)
  87.  
  88.         # Button Browse
  89.         abtn = Button(self, text="Browse", command=lambda:BrowseButtonClass(value))
  90.         abtn.grid(row=1, column=3)
  91.  
  92.         #Button Close
  93.         cbtn = Button(self, text="Close", command=CloseButtonClass)
  94.         cbtn.grid(row=2, column=3, pady=4)
  95.  
  96.         #Button Help
  97.         hbtn = Button(self, text="Help", command=HelpButtonClass)
  98.         hbtn.grid(row=5, column=0, padx=5)
  99.  
  100.         #Button Go
  101.         obtn = Button(self, text="GO", command=GoButtonClass)
  102.         obtn.grid(row=5, column=3)
  103.  
  104.  
  105. def main():
  106.  
  107.     root = Tk()
  108.     root.geometry("300x200+300+300")
  109.     app = MapillaryGui(root)
  110.     root.mainloop()
  111.  
  112.  
  113. if __name__ == '__main__':
  114.     main()
  115.  
Jul 16 '15 #2

P: 5
great work .. thanx.. that was helping
Jul 22 '15 #3

Post your reply

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