473,386 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

update of elements in GUI (TKinter)

hi,
here's the situation: I'm trying to build a GUI for a Genetic Algorithm that has a number of elements (labels) updated every iteration of the run.

I've tried doing it in the widget (Label) (1) and in the mainframe (2), but neither worked.

(1)
Expand|Select|Wrap|Line Numbers
  1. Label(mainframe.update_idletasks(),textvariable=best,bg='#321000',fg='#000fff000',font=("Helvetica",x1)).grid(column=1,row=1)
(2)
Expand|Select|Wrap|Line Numbers
  1. mainframe = Frame(root)
  2. mainframe.grid(column=1000, row=1000, sticky=(N, W, E, S))
  3. mainframe.columnconfigure(0, weight=1)
  4. mainframe.rowconfigure(0, weight=1)
  5. mainframe.update()
  6.  
Thanks for any ideas
Aug 18 '10 #1
7 33271
dwblas
626 Expert 512MB
You use a Tkinter variable since they are different from a Python variable, set() the new value (which converts to a Tkinter variable), and update the widget. The following updates a counter every second and displays it on the screen. If this does not solve the problem, post back with more detail (but thanks for not posting the thousands of lines of code for the Genetic Algorithm ). Note that update is considered by some to be bad coding style and you should call some event. But sometimes using mutiprocessing or threading isn't a better choice .
Expand|Select|Wrap|Line Numbers
  1. import time
  2. from Tkinter import *
  3.  
  4. root = Tk()
  5. root.geometry('100x50+20+20')
  6.  
  7. mainframe = Frame(root)
  8. mainframe.grid(column=1000, row=1000, sticky=(N, W, E, S))
  9. mainframe.columnconfigure(0, weight=1)
  10. mainframe.rowconfigure(0, weight=1)
  11.  
  12. best = StringVar()
  13. best.set('start')
  14. x1 = 12
  15. Label(mainframe,textvariable=best,bg='#321000',fg='#000fff000',font=("Helvetica",x1)).grid(column=1,row=1)
  16.  
  17. for x in range(10):
  18.     time.sleep(1.0)
  19.     best.set('test # %d' % (x))
  20.     mainframe.update()
  21. root.mainloop() 
Aug 18 '10 #2
bvdet
2,851 Expert Mod 2GB
StringVar() best can be updated using StringVar() method set(). Any widget whose textvariable option is slaved to this variable will be updated when the main loop next idles.
Aug 18 '10 #3
bvdet
2,851 Expert Mod 2GB
You can get away from the update() method by using after(). Following is dwblas example, modified for after():
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. root = Tk()
  4. root.geometry('100x50+20+20')
  5.  
  6. mainframe = Frame(root)
  7. mainframe.grid(column=1000, row=1000, sticky=(N, W, E, S))
  8. mainframe.columnconfigure(0, weight=1)
  9. mainframe.rowconfigure(0, weight=1)
  10.  
  11. best = StringVar()
  12. best.set('start')
  13. x1 = 12
  14. label1 = Label(mainframe,textvariable=best,bg='#321000',fg='#000fff000',font=("Helvetica",x1))
  15. label1.grid()
  16.  
  17. def run(n, master):
  18.     best.set('test # %d' % (n))
  19.     n += 1
  20.     if n <= 10:
  21.         mainframe.after(1000, run, n, master)
  22.  
  23. run(0, mainframe)
  24. root.mainloop()
Aug 18 '10 #4
hey thanks this seems to work...one more question, if u don't maind, what if I'd like to launch this counter at a click of a button? I've tried modifying your code

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. root = Tk()
  4. root.geometry('100x50+20+20')
  5.  
  6. mainframe = Frame(root)
  7. mainframe.grid(column=1000, row=1000, sticky=(N, W, E, S))
  8. mainframe.columnconfigure(0, weight=1)
  9. mainframe.rowconfigure(0, weight=1)
  10.  
  11. best = StringVar()
  12. best.set('start')
  13. x1 = 12
  14. label1 = Label(mainframe,textvariable=best,bg='#321000',fg='#000fff000',font=("Helvetica",x1))
  15. label1.grid()
  16.  
  17. def run(*args):
  18.     n=0;master=mainframe
  19.     best.set('test # %d' % (n))
  20.     n += 1
  21.     if n <= 10:
  22.         mainframe.after(1000, run, n, master)
  23.  
  24. but1=Button(text="RUN",command=run)
  25. bu1.grid() 
  26. run(0, mainframe)
  27. root.mainloop()
  28.  
but apparently it didn't work out. Sorry, I;m very new to both Python and GUI. The algorithm without this iterative update trick works just fine.
Aug 19 '10 #5
bvdet
2,851 Expert Mod 2GB
A little modification:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. font1 = ("Helvetica",12)
  4.  
  5. root = Tk()
  6. root.geometry('100x50+20+20')
  7.  
  8. mainframe = Frame(root)
  9. mainframe.grid(column=1000, row=1000, sticky=(N, W, E, S))
  10. mainframe.columnconfigure(0, weight=1)
  11. mainframe.rowconfigure(0, weight=1)
  12.  
  13. best = StringVar()
  14. best.set('start')
  15. label1 = Label(mainframe,textvariable=best,bg='#321000',fg='#000fff000',font=font1)
  16. label1.grid()
  17.  
  18. def run(n=0, master=mainframe):
  19.     best.set('test # %d' % (n))
  20.     n += 1
  21.     if n <= 10:
  22.         mainframe.after(1000, run, n, master)
  23.  
  24. but1=Button(text="RUN",command=run)
  25. but1.grid()
  26. root.mainloop()
  27.  
Aug 19 '10 #6
OK thanx a great lot, this actually did work, but there seems to be a bit of a problem: the function [run] is called every time from scratch, i.e. if I update an array (pretty much how a genetic algorithm works) I need to store it for the next iteration to use. Is this somehow doable in this case?

I see that you start with n=0 and then add an increment every iteration until some condition is fulfilled, so each time [run] is called n is not defaulted to 0, so this should work with arrays too...

Sorry for this being messy, but I've written the algorithm some time ago and this is the only thing that doesn't work and gets me concerned.

thanks again!
Aug 19 '10 #7
Sorry for this, I actually sorted the way out)

thnx 2 everyone
Aug 19 '10 #8

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

Similar topics

0
by: Russell E. Owen | last post by:
I have a Python/Tkinter remote control application and want to add support for running user scripts. I would like the scripts to be written in python, would like them to be able to update...
2
by: Stewart Midwinter | last post by:
I would like to link the contents of three OptionMenu lists. When I select an item from the first list (call it continents), the contents of the 2nd list (call it countries) would update. And in...
4
by: Jeffrey Barish | last post by:
I'm confused about how to use the update_idletasks method. In my program, I have a handler for a button in which execution will linger. During that time, I would like for the GUI to continue to...
2
by: Michael Zhang | last post by:
My project uses Python-2.3.4 + Tkinter + PIL-1.1.4 to retrieve images from server and display those images. I created a thread (also a separate toplevel window) for displaying images and another...
7
by: Harlin Seritt | last post by:
I was looking at the Tcl/Tk sourceforge page and found that there were a couple of new widgets being produced for Tcl 8.5. Does anyone know if there are any Tkinter wrappers somewhere? thanks, ...
1
by: C D Wood | last post by:
To whom this may concern, Below is the source code, which demonstrates a problem I am having making a GUI for my python project work. 'table.txt' is a file that is read from the same folder....
2
by: Kevin Walzer | last post by:
I'm trying to decide whether I need threads in my Tkinter application or not. My app is a front end to a command-line tool; it feeds commands to the command-line program, then reads its output and...
0
kudos
by: kudos | last post by:
Hi, a couple of months ago, I made a xmas chistmas "card" to this community(http://www.thescripts.com/forum/thread580722.html) Now its soon easter, and I hacked together something for easter as...
2
by: Kevin Walzer | last post by:
I'm porting a Tkinter application to wxPython and had a question about wxPython's event loop. The Tkinter app provides a GUI to a command-line tool. It gathers user input, and opens an...
44
by: bg_ie | last post by:
Hi, I'm in the process of writing some code and noticed a strange problem while doing so. I'm working with PythonWin 210 built for Python 2.5. I noticed the problem for the last py file...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.