[Tutor] Clock in tkinter?
Mic
o0MB0o at hotmail.se
Wed Nov 16 16:28:57 CET 2011
Hi!
It is no homework, in fact am I working ahead of class. I have now, after five minutes thinking, solved my problem, but a new problem has risen.
But to begin with, here is how I solved the problem:
from tkinter import*
import time
the_time=''
class Window(Frame):
def __init__(self,master):
super(Window,self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
#Create a hello button:
hej=self.hellobttn=Button(self,text="Hey")
self.hellobttn.grid(row=0, column=0)
#Create a label that displays time:
self.display_time=Label(self, text=the_time)
self.display_time.grid(row=0, column=1)
def change_value_the_time():
global the_time
newtime = time.strftime('%H:%M:%S')
if newtime != the_time:
the_time= newtime
self.display_time.config(text=the_time, font="40")
self.display_time.after(20, change_value_the_time)
change_value_the_time()
root=Tk()
root.title("Test")
root.geometry("200x200")
app=Window(root)
root.mainloop()
I found some help on the internet about making a clock, although I had to modify a lot of the code to fit my own code and window.
Now to my next question. Say that I want a text “Hi, how are you?” to be printed when the time passes 15:00:00 each day. How do I do that?
At first I thought that I could just write an if statement. Like:
if the_time>15:00:00:
print (“Hi, how are you?”)
But it is obviously not working.
Thank you for your help! Another question, am I supposed to add tutor at python.orgtutor@python.org; as copy? You did that, right?
Regards Mic
From: Wayne Werner
Sent: Tuesday, November 15, 2011 10:14 PM
To: Mic
Cc: tutor at python.org
Subject: Re: [Tutor] Clock in tkinter?
On Tue, Nov 15, 2011 at 2:00 PM, Mic <o0MB0o at hotmail.se> wrote:
Hi!
I am new to programming and I hop this question isn’t stupid.
Welcome!
I am making a small GUI program. It is supposed to have a button and a clock in it that displays the same time as it is according to the computer.
So as far as I am concerned both the clock and the are supposed to be widgets?
Is this a homework assignment, or just something that you're doing for fun? It seems homework-ish. We don't mind pointing you in the right direction on homework assignments, but we definitely won't do it for you.
So how do I add this clock as a widget placed next to the button?
Tkinter doesn't have a native clock widget, so if you want to make a clock you need to roll your own.
Here is the code I have written so far, that only displays a GUI window, and a button:
from tkinter import *
import time
class Window(Frame):
def __init__(self,master):
super(Window,self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.test_button=Button(self, text="Hi")
self.test_button.grid(row=0,column=0)
root=Tk()
root.title("Test")
root.geometry("200x200")
app=Window(root)
root.mainloop()
Giving us code (especially such a small amount) is exactly the right thing to do when you ask a question - it shows that you've tried something, and if it's broken it usually shows why it's broken.
You can add text to the Label widget - and you can change the text on that widget.
You're already importing the time module - for more information about what it contains, you can run the interactive interpreter and do this:
>>> import time
>>> help(time)
Or you can look online for the commands that might help you.
If you get stuck, let us know what you're doing and where you're stuck at.
HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111116/85f143a6/attachment-0001.html>
More information about the Tutor
mailing list