hi guys.. ive tried to integrate a timer code (which works fine) into a tkinter window but im having errors. can u plz fix it 4 me? thanks.

this is the code i want to integrate.

import Tkinter as tk
    import time

    def start():
        global count_flag
        count_flag = True
        count = 0.0
        while True:
            if count_flag == False:
                break
            # put the count value into the label
            label['text'] = str(count)
            # wait for 0.1 seconds
            time.sleep(0.1)
            # needed with time.sleep()
            root.update()
            # increase count
            count += 0.1

    def stop():
        global count_flag
        count_flag = False


    # create a Tkinter window
    root = tk.Tk()

    # this will be a global flag
    count_flag = True

    # create needed widgets
    label = tk.Label(root, text='0.0')
    btn_start = tk.Button(root, text='start', command=start)
    btn_stop = tk.Button(root, text='stop', command=stop)

    # use a grid to place the widgets
    label.grid(row=0, column=0, columnspan=2)
    btn_start.grid(row=1, column=0, padx=5, pady=5)
    btn_stop.grid(row=1, column=1, padx=5)

    # start the event loop
    root.mainloop()

i want to integrate the above code in this tkinter a window but its not working.

import Tkinter as tk
    import time
    import sys,Queue
    import datetime as dt
    from Tkinter import *


    class stopwatch(Label):
        def __init__(self, parent, *args, **kwargs):
            Label.__init__(self, parent, *args, **kwargs)
            btn_start = Button(root, text='start', command=self.start)
            btn_stop = Button(root, text='stop', command=self.stop)
            canvas.grid(row=0, column=0, columnspan=2)
            btn_start.grid(row=1, column=0, padx=5, pady=5)
            btn_stop.grid(row=1, column=1, padx=5)

        def start(self):
            global count_flag
            count_flag = True
            count = 0.0
            while True:
                if count_flag == False:
                    break
                # put the count value into the label
                label['text'] = str(count)
                # wait for 0.1 seconds
                time.sleep(0.1)
                # needed with time.sleep()
                root.update()
                # increase count
                count += 0.1

        def stop(self):
            global count_flag
            count_flag = False


    # create a Tkinter window

    root=tk.Tk()

    canvas = tk.Canvas(root, width=1000, height=300, bg="#FFFFFF")
    stopwatch=stopwatch(root, text='0.0')
    stopwatch.pack(fill=BOTH, expand=1)
    canvas.pack()
    # this will be a global flag
    count_flag = True

    # create needed widgets
    #label = tk.Label(root, text='0.0')


    # use a grid to place the widgets
    #label.grid(row=0, column=0, columnspan=2)


    # start the event loop
    root.mainloop()

i want to integrate the above code in this tkinter a window but its not working.

What's not working? Are you getting an error? Your "tkinter a window" doesn't work for me. It never generates any windows and just hangs, so I'm not sure what you're looking for.

actually im going to present you another piece of code. forget the above codes. what i want is to display the the time elapsed when i click 'start'. thanks

import Tkinter as tk
import time

def start():
    global count_flag
    count_flag = True
    count = 0.0
    while True:
        if count_flag == False:
            break
        # put the count value into the label
        label['text'] = str(count)
        # wait for 0.1 seconds
        time.sleep(0.1)
        # needed with time.sleep()
        root.update()
        # increase count
        count += 0.1

def stop():
    global count_flag
    count_flag = False


# create a Tkinter window
root = tk.Tk()
canvas = tk.Canvas(root, width=1000, height=300, bg="#FFFFFF")
canvas.pack()
# this will be a global flag
count_flag = True

# create needed widgets
label = tk.Label(root, text='0.0')
btn_start = tk.Button(root, text='start', command=start)
btn_stop = tk.Button(root, text='stop', command=stop)

# use a grid to place the widgets
canvas.grid(row=0, column=0, columnspan=2)
btn_start.grid(row=1, column=0, padx=5, pady=5)
btn_stop.grid(row=1, column=1, padx=5)

# start the event loop
root.mainloop()

You need to place the label somewhere like you did with the buttons:

label.grid(row=1, column=2)

Also change:

while True:
    if count_flag == False:

To:

while count_flag:

Although that's not necessary, it's proper use of while loops.

Here's a working version of the classed code from above (I incorporated adam's suggestions from above and modified some behavior slightly):

import Tkinter as tk
import time

class stopwatch( object ):
    def __init__(self, parent, *args, **kwargs):
        self.parent = parent
        self.count_flag = False
        canvas = tk.Canvas(self.parent, *args, **kwargs)
        canvas.pack()
        # create needed widgets
        self.counter = tk.Label(self.parent, text='0.0')
        btn_start = tk.Button(self.parent, text='start', command=self.start)
        btn_stop = tk.Button(self.parent, text='stop', command=self.stop)
        # use a grid to place the widgets
        canvas.grid(row=0, column=0, columnspan=2)
        self.counter.grid(row=0, column=0, columnspan=2)
        btn_start.grid(row=1, column=0, padx=5, pady=5)
        btn_stop.grid(row=1, column=1, padx=5)

    def start(self):
        self.count_flag = True
        count = 0.0
        while self.count_flag:
            # put the count value into the label
            self.counter['text'] = str(count)
            # wait for 0.1 seconds
            time.sleep(0.1)
            # needed with time.sleep()
            self.parent.update()
            # increase count
            count += 0.1

    def stop(self):
        self.count_flag = False

def main():
    # create a Tkinter window
    root = tk.Tk()
    sw = stopwatch( root, width=50, height=40, bg="#A3A3A3" )
    # start the event loop
    root.mainloop()

if __name__ == '__main__':
    main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.