[Tkinter-discuss] Tkinter-discuss Digest, Vol 15, Issue 1

Harlin Seritt harlinseritt at yahoo.com
Tue May 3 12:38:23 CEST 2005


Hi Ian,
 
I had some trouble doing something like this when I was trying to write a balloon widget. I was using while, threading, etc... Try changing your act() method to this:
 
    def act(self):
        self.ring = self.stringv.get()+self.stringv2.get()+":"\
                    +self.stringv3.get()+self.stringv4.get()
        self.track = [None]
        self.c = self.clock()
        self.stringv.set(self.c[0])
        self.stringv2.set(self.c[1])
        self.stringv3.set(self.c[3])
        self.stringv4.set(self.c[4])
 
          
        self.c = self.clock()    
        if self.c != self.ring:
            if self.c == self.track[-1]: pass
            else:
                self.stringv.set(self.c[0])
                self.stringv2.set(self.c[1])
                self.stringv3.set(self.c[3])
                self.stringv4.set(self.c[4])
                self.track.append(self.c)
        else: winsound.PlaySound("rooster.wav", winsound.SND_LOOP)
        
        self.master.after(1000, self.act)
 
Tk().after(milliseconds, method/function to call) is great for this sort of thing.
 
Good luck!
 
Harlin Seritt


tkinter-discuss-request at python.org wrote:
Send Tkinter-discuss mailing list submissions to
tkinter-discuss at python.org

To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/tkinter-discuss
or, via email, send a message with subject or body 'help' to
tkinter-discuss-request at python.org

You can reach the person managing the list at
tkinter-discuss-owner at python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tkinter-discuss digest..."


Today's Topics:

1. Problem looping (Brill IanT)
2. Re: Problem looping (Cameron Laird)


----------------------------------------------------------------------

Message: 1
Date: Mon, 2 May 2005 17:41:50 +0100 (BST)
From: Brill IanT 
Subject: [Tkinter-discuss] Problem looping
To: tkinter-discuss at python.org
Message-ID: <20050502164151.17812.qmail at web26606.mail.ukl.yahoo.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi all! 

I was wondering if anyone could help me out. 
I wrote a little alarm clock program in Python, and now want to incorporate it into a Tkinter frame. It's all going quite well, but this is my first experience with Tk and I've hit a bit of a wall. One of the button's commands refers to a method (called 'act') which has a while loop within it (keeps track of the time and ought to display it in the Tk frame). But the problem is the while loop is interupting the root mainloop, so the labels will not update, and freezes until the while loop is over. 
Does anyone have any suggestions to get round this?

Here's the program:

import time, winsound
from Tkinter import *

class App:
def __init__(self,master):
self.master = master
frame = Frame(master,bd=6,relief=RIDGE)
frame.pack()
frame2 = Frame(master)
frame2.pack()

self.list = ["9","8","7","6","5","4","3","2","1","0"]
self.stringv = StringVar()
self.stringv.set("0")
self.stringv2 = StringVar()
self.stringv2.set("0")
self.stringv3 = StringVar()
self.stringv3.set("0")
self.stringv4 = StringVar()
self.stringv4.set("0")

self.lab1 = Label(frame,font=("helvica",22),bg="red",
textvariable=self.stringv).pack(side=LEFT)
self.b1 = Button(frame2,text="H",width=3,command=self.change)
self.b1.pack(side=LEFT)

self.lab2 = Label(frame,font=("helvica",22),bg="red",
textvariable=self.stringv2).pack(side=LEFT)
self.b2 = Button(frame2,text="h",width=3,command=self.change2)
self.b2.pack(side=LEFT)

self.lab3 = Label(frame,text=":",font=("helvica",22),
bg="red",).pack(side=LEFT)

self.lab4 = Label(frame,font=("helvica",22),bg="red",
textvariable=self.stringv3).pack(side=LEFT)
self.b3 = Button(frame2,text="M",width=3,command=self.change3)
self.b3.pack(side=LEFT)

self.lab5 = Label(frame,font=("helvica",22),bg="red",
textvariable=self.stringv4).pack(side=LEFT)
self.b4 = Button(frame2,text="m",width=3,command=self.change4)
self.b4.pack(side=LEFT)

self.set_button = Button(master,text="Set",width=20,
command=self.act).pack()

def change(self):
if int(self.stringv2.get()) < 3:
self.list1 = self.list[7:]
i = self.list1.index(self.stringv.get())
self.stringv.set(self.list1[i-1])
else:
self.list1 = self.list[8:]
i = self.list1.index(self.stringv.get())
self.stringv.set(self.list1[i-1])

def change2(self):
self.list2 = self.list[6:]
if self.stringv.get() == "2":
i = self.list2.index(self.stringv2.get())
self.stringv2.set(self.list2[i-1])
else: 
i = self.list.index(self.stringv2.get())
self.stringv2.set(self.list[i-1])

def change3(self):
self.list3 = self.list[4:]
i = self.list3.index(self.stringv3.get())
self.stringv3.set(self.list3[i-1])

def change4(self):
i = self.list.index(self.stringv4.get())
self.stringv4.set(self.list[i-1])

def act(self):
self.ring = self.stringv.get()+self.stringv2.get()+":"\
+self.stringv3.get()+self.stringv4.get()
self.track = [None]
self.c = self.clock()
self.stringv.set(self.c[0])
self.stringv2.set(self.c[1])
self.stringv3.set(self.c[3])
self.stringv4.set(self.c[4])

while 1:
self.c = self.clock() 
if self.c != self.ring:
if self.c == self.track[-1]: pass
else:
self.stringv.set(self.c[0])
self.stringv2.set(self.c[1])
self.stringv3.set(self.c[3])
self.stringv4.set(self.c[4])
self.track.append(self.c)
else: winsound.PlaySound("rooster.wav", winsound.SND_LOOP)

def clock(self):
t1 = time.localtime()[3:5]
if t1[0] in range(10): tm1 = '0'+ str(t1[0])
else: tm1 = str(t1[0])

if t1[1] in range(10): tm2 = '0'+ str(t1[1])
else: tm2 = str(t1[1])

tm3 = tm1 + ":" + tm2
return tm3

if __name__ == "__main__":
root=Tk()
root.title("")
root.geometry("100x97+300+200")
App(root)
root.mainloop()


Thanks in advance.


Send instant messages to your online friends http://uk.messenger.yahoo.com 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20050502/77610d5d/attachment.html

------------------------------

Message: 2
Date: Mon, 2 May 2005 16:44:54 +0000
From: Cameron Laird 
Subject: Re: [Tkinter-discuss] Problem looping
To: Brill IanT 
Cc: tkinter-discuss at python.org
Message-ID: <20050502164454.GA15435 at lairds.us>
Content-Type: text/plain; charset=us-ascii

On Mon, May 02, 2005 at 05:41:50PM +0100, Brill IanT wrote:
.
.
.
> I was wondering if anyone could help me out. 
> I wrote a little alarm clock program in Python, and now want to incorporate it into a Tkinter frame. It's all going quite well, but this is my first experience with Tk and I've hit a bit of a wall. One of the button's commands refers to a method (called 'act') which has a while loop within it (keeps track of the time and ought to display it in the Tk frame). But the problem is the while loop is interupting the root mainloop, so the labels will not update, and freezes until the while loop is over. 
.
.
.
Short answer: .

Do you want help translating this into Tkinter?


------------------------------

_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss


End of Tkinter-discuss Digest, Vol 15, Issue 1
**********************************************

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20050503/388e609f/attachment.html


More information about the Tkinter-discuss mailing list