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) - Label(mainframe.update_idletasks(),textvariable=best,bg='#321000',fg='#000fff000',font=("Helvetica",x1)).grid(column=1,row=1)
(2) - mainframe = Frame(root)
-
mainframe.grid(column=1000, row=1000, sticky=(N, W, E, S))
-
mainframe.columnconfigure(0, weight=1)
-
mainframe.rowconfigure(0, weight=1)
-
mainframe.update()
-
Thanks for any ideas
7 33271
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 . - import time
-
from Tkinter import *
-
-
root = Tk()
-
root.geometry('100x50+20+20')
-
-
mainframe = Frame(root)
-
mainframe.grid(column=1000, row=1000, sticky=(N, W, E, S))
-
mainframe.columnconfigure(0, weight=1)
-
mainframe.rowconfigure(0, weight=1)
-
-
best = StringVar()
-
best.set('start')
-
x1 = 12
-
Label(mainframe,textvariable=best,bg='#321000',fg='#000fff000',font=("Helvetica",x1)).grid(column=1,row=1)
-
-
for x in range(10):
-
time.sleep(1.0)
-
best.set('test # %d' % (x))
-
mainframe.update()
-
root.mainloop()
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.
bvdet 2,851
Expert Mod 2GB
You can get away from the update() method by using after(). Following is dwblas example, modified for after(): - from Tkinter import *
-
-
root = Tk()
-
root.geometry('100x50+20+20')
-
-
mainframe = Frame(root)
-
mainframe.grid(column=1000, row=1000, sticky=(N, W, E, S))
-
mainframe.columnconfigure(0, weight=1)
-
mainframe.rowconfigure(0, weight=1)
-
-
best = StringVar()
-
best.set('start')
-
x1 = 12
-
label1 = Label(mainframe,textvariable=best,bg='#321000',fg='#000fff000',font=("Helvetica",x1))
-
label1.grid()
-
-
def run(n, master):
-
best.set('test # %d' % (n))
-
n += 1
-
if n <= 10:
-
mainframe.after(1000, run, n, master)
-
-
run(0, mainframe)
-
root.mainloop()
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 -
from Tkinter import *
-
-
root = Tk()
-
root.geometry('100x50+20+20')
-
-
mainframe = Frame(root)
-
mainframe.grid(column=1000, row=1000, sticky=(N, W, E, S))
-
mainframe.columnconfigure(0, weight=1)
-
mainframe.rowconfigure(0, weight=1)
-
-
best = StringVar()
-
best.set('start')
-
x1 = 12
-
label1 = Label(mainframe,textvariable=best,bg='#321000',fg='#000fff000',font=("Helvetica",x1))
-
label1.grid()
-
-
def run(*args):
-
n=0;master=mainframe
-
best.set('test # %d' % (n))
-
n += 1
-
if n <= 10:
-
mainframe.after(1000, run, n, master)
-
-
but1=Button(text="RUN",command=run)
-
bu1.grid()
-
run(0, mainframe)
-
root.mainloop()
-
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.
bvdet 2,851
Expert Mod 2GB
A little modification: - from Tkinter import *
-
-
font1 = ("Helvetica",12)
-
-
root = Tk()
-
root.geometry('100x50+20+20')
-
-
mainframe = Frame(root)
-
mainframe.grid(column=1000, row=1000, sticky=(N, W, E, S))
-
mainframe.columnconfigure(0, weight=1)
-
mainframe.rowconfigure(0, weight=1)
-
-
best = StringVar()
-
best.set('start')
-
label1 = Label(mainframe,textvariable=best,bg='#321000',fg='#000fff000',font=font1)
-
label1.grid()
-
-
def run(n=0, master=mainframe):
-
best.set('test # %d' % (n))
-
n += 1
-
if n <= 10:
-
mainframe.after(1000, run, n, master)
-
-
but1=Button(text="RUN",command=run)
-
but1.grid()
-
root.mainloop()
-
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!
Sorry for this, I actually sorted the way out)
thnx 2 everyone
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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,
...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |