Python 3.x 结合tkinter和turtle,闹钟python。
我制作了这个闹钟,但似乎无法将我的乌龟图案与tkinder结合起来,至少不是全部。此外,我似乎无法让我的闹钟响应估算的日期时间 闹钟 需要帮助快 我。。。似乎无法将我的海龟图案与tkinder结合起来 我的建议是你扔掉tkinter部分,完全用turtle来做。对于报警设置,您似乎只需要tkinter,我在下面使用turtle作为按钮和Python 3中引入的Python 3.x 结合tkinter和turtle,闹钟python。,python-3.x,turtle-graphics,tkinter-canvas,python-datetime,Python 3.x,Turtle Graphics,Tkinter Canvas,Python Datetime,我制作了这个闹钟,但似乎无法将我的乌龟图案与tkinder结合起来,至少不是全部。此外,我似乎无法让我的闹钟响应估算的日期时间 闹钟 需要帮助快 我。。。似乎无法将我的海龟图案与tkinder结合起来 我的建议是你扔掉tkinter部分,完全用turtle来做。对于报警设置,您似乎只需要tkinter,我在下面使用turtle作为按钮和Python 3中引入的textinput(title,prompt)函数(我也对您的代码进行了一些修改)重新进行了设置: 我似乎无法让我的闹钟响应估算的日期时间
textinput(title,prompt)
函数(我也对您的代码进行了一些修改)重新进行了设置:
我似乎无法让我的闹钟响应估算的日期时间
我已经暗示了这在我的代码设计中是如何工作的,但我还没有测试它。我没有winsound库,这就是我注释掉winsound相关代码的原因——我知道它没有任何问题
import winsound
import turtle
from turtle import *
from datetime import datetime
import tkinter
from tkinter import *
#combine turtle and tkinder
root = tkinter.Tk()
root.title("Alarm Clock")
cv = tkinter.Canvas(root, width=700, height=700)
cv.pack(side = tkinter.LEFT)
sc = turtle.RawTurtle(cv)
sc.setpos(0, -200)
s = sc.getscreen()
s.delay(0)
#turtle draws a circle
for aColor in ["black", "black", "black", "black"]:
sc.color(aColor)
sc.pensize(3)
def drawPoly(t, num_sides, side_length):
for i in range(num_sides):
t.forward(side_length)
t.left(90 / num_sides)
drawPoly(sc, 320, 1)
sc.color("white")
sc.fd(100)
def jump(distanz, winkel=0):
penup()
right(winkel)
forward(distanz)
left(winkel)
pendown()
def hand(laenge, spitze):
fd(laenge*1.10)
rt(90)
fd(spitze/20.0)
lt(120)
fd(spitze)
lt(120)
fd(spitze)
lt(120)
fd(spitze/20.0)
def make_hand_shape(name, laenge, spitze):
reset()
jump(-laenge*0.15)
begin_poly()
hand(laenge, spitze)
end_poly()
hand_form = get_poly()
register_shape(name, hand_form)
def clockface(radius):
reset()
pencolor("black")
pensize(7)
for i in range(60):
jump(radius)
if i % 5 == 0:
fd(25)
jump(-radius-25)
else:
dot(4)
jump(-radius)
rt(6)
def setup():
global second_hand, minute_hand, hour_hand, sc
mode("logo")
#clock hands
make_hand_shape("second_hand", 125, 5)
make_hand_shape("minute_hand", 130, 5)
make_hand_shape("hour_hand", 90, 5)
clockface(160)
second_hand = Turtle()
second_hand.shape("second_hand")
second_hand.color("grey", "grey")
minute_hand = Turtle()
minute_hand.shape("minute_hand")
minute_hand.color("#505050", "#505050")
hour_hand = Turtle()
hour_hand.shape("hour_hand")
hour_hand.color("#191919", "#191919")
for hand in second_hand, minute_hand, hour_hand:
hand.resizemode("user")
hand.shapesize(1, 1, 3)
hand.speed(0)
ht()
sc.ht()
sc.pu()
sc.bk(85)
def wochentag(t):
wochentag = ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"]
return wochentag[t.weekday()]
#date
def datum(z):
monat = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June",
"July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]
j = z.year
m = monat[z.month - 1]
t = z.day
return "%s %d %d" % (m, t, j)
def tick():
t = datetime.today()
sekunde = t.second + t.microsecond*0.000001
minute = t.minute + sekunde/60.0
stunde = t.hour + minute/60.0
try:
tracer(False) # Terminator can occur here
sc.setpos(0, 100)
sc.clear()
sc.home()
sc.forward(65)
sc.pencolor("grey")
sc.write(wochentag(t),
align="center", font=("Courier", 14, "bold"))
sc.back(150)
sc.write(datum(t),
align="center", font=("Courier", 14, "bold"))
sc.goto(-20, 200)
d = datetime.now().strftime("%Y-%m-%d %H:%M")
current_time = datetime.strptime(d, "%Y-%m-%d %H:%M").strftime("%Y-%m-%d %I:%M %p")
sc.write(current_time,
align="center", font=("Courier", 14, "bold"))
tracer(True)
second_hand.setheading(6*sekunde) # or here
minute_hand.setheading(6*minute)
hour_hand.setheading(30*stunde)
tracer(True)
ontimer(tick, 100)
except Terminator:
pass
def main():
tracer(False)
setup()
tracer(True)
tick()
return "EVENTLOOP"
if __name__ == "__main__":
mode("logo")
main()
d = datetime.now().strftime("%Y-%m-%d %H:%M")
current_time = (datetime.strptime(d, "%Y-%m-%d %H:%M").strftime("%Y-%m-%d %I:%M %p"))
print (datetime.strptime(d, "%Y-%m-%d %H:%M").strftime("%Y-%m-%d %I:%M %p"))
#combine turtle and tkinder
frame = tkinter.Frame(root)
frame.pack(side = tkinter.LEFT,fill=tkinter.BOTH)
pointLabel = tkinter.Label(frame,text="Set Alarm")
pointLabel.pack()
#button sets imputed time and date for alarm to start
pointSize = tkinter.StringVar()
pointEntry = tkinter.Entry(frame,textvariable=pointSize)
pointEntry.pack()
pointSize.set(current_time)
alarm_clock_dic = []
def quitHandler():
alarm_clock_dic.append(pointSize)
cmd_Button = tkinter.Button(frame, text = "Enter Time", command=quitHandler,
bg ='grey', fg = 'white')
cmd_Button.pack()
if alarm_clock_dic == (current_time):
winsound.PlaySound("SystemExit", winsound.SND_LOOP)
winsound.PlaySound("SystemExit", winsound.SND_LOOP)
winsound.PlaySound("SystemExit", winsound.SND_LOOP)
winsound.PlaySound("SystemExit", winsound.SND_LOOP)
winsound.PlaySound("SystemExit", winsound.SND_LOOP)
mainloop()
# import winsound
from turtle import Turtle, Screen, Terminator
from datetime import datetime
screen = Screen()
screen.mode('logo')
screen.setup(700, 700)
screen.title('Alarm Clock')
wochentag_list = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
monat = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'June', 'July', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.']
FONT_SIZE = 14
FONT = ('Courier', FONT_SIZE, 'bold')
BUTTON_SIZE = 40
CURSOR_SIZE = 20
def jump(turtle, distanz):
turtle.penup()
turtle.forward(distanz)
turtle.pendown()
def hand(turtle, laenge, spitze):
turtle.fd(laenge * 1.10)
turtle.rt(90)
turtle.fd(spitze / 20.0)
turtle.lt(120)
turtle.fd(spitze)
turtle.lt(120)
turtle.fd(spitze)
turtle.lt(120)
turtle.fd(spitze / 20.0)
def make_hand_shape(turtle, name, laenge, spitze):
turtle.reset()
jump(turtle, -laenge * 0.15)
turtle.begin_poly()
hand(turtle, laenge, spitze)
turtle.end_poly()
screen.register_shape(name, turtle.get_poly())
def clockface(radius):
face = Turtle()
face.pensize(3)
face.speed('fastest')
jump(face, radius)
face.left(90)
face.circle(radius)
face.right(90)
jump(face, -radius)
face.pensize(7)
radius *= 0.8
jump(face, radius)
for i in range(60):
if i % 5 == 0:
face.forward(25)
jump(face, -25)
else:
face.dot(4)
face.left(90)
face.penup()
face.circle(radius, 6)
face.pendown()
face.right(90)
face.hideturtle()
def setup():
hand = Turtle()
# clock hands
make_hand_shape(hand, 'second_hand', 125, 5)
make_hand_shape(hand, 'minute_hand', 130, 5)
make_hand_shape(hand, 'hour_hand', 90, 5)
hand.reset()
hand.hideturtle()
clockface(200)
second_hand = Turtle()
second_hand.shape('second_hand')
second_hand.color('grey')
minute_hand = Turtle()
minute_hand.shape('minute_hand')
minute_hand.color('#505050')
hour_hand = Turtle()
hour_hand.shape('hour_hand')
hour_hand.color('#191919')
for hand in second_hand, minute_hand, hour_hand:
hand.shapesize(outline=3)
hand.speed('fastest')
return second_hand, minute_hand, hour_hand
def wochentag(t):
return wochentag_list[t.weekday()]
# date
def datum(z):
j = z.year
m = monat[z.month - 1]
t = z.day
return '%s %d %d' % (m, t, j)
def tick():
t = datetime.today()
sekunde = t.second + t.microsecond * 0.000001
minute = t.minute + sekunde / 60.0
stunde = t.hour + minute / 60.0
d = datetime.now().strftime('%Y-%m-%d %H:%M')
current_time = datetime.strptime(d, '%Y-%m-%d %H:%M').strftime('%Y-%m-%d %I:%M %p')
try:
marker1.undo()
marker1.write(wochentag(t), align='center', font=FONT)
marker2.undo()
marker2.write(datum(t), align='center', font=FONT)
marker3.undo()
marker3.write(current_time, align='center', font=FONT)
second_hand.setheading(6 * sekunde)
minute_hand.setheading(6 * minute)
hour_hand.setheading(30 * stunde)
screen.update()
if current_time in alarm_clock_dic:
#winsound.PlaySound('SystemExit', winsound.SND_LOOP)
#winsound.PlaySound('SystemExit', winsound.SND_LOOP)
#winsound.PlaySound('SystemExit', winsound.SND_LOOP)
#winsound.PlaySound('SystemExit', winsound.SND_LOOP)
#winsound.PlaySound('SystemExit', winsound.SND_LOOP)
pass
screen.ontimer(tick, 100)
except Terminator:
screen.bye()
def set_alarm(x, y):
d = datetime.now().strftime('%Y-%m-%d %H:%M')
current_time = (datetime.strptime(d, '%Y-%m-%d %H:%M').strftime('%Y-%m-%d %I:%M %p'))
pointSize = screen.textinput(current_time, "Enter Time")
alarm_clock_dic.append(pointSize)
marker1 = Turtle(visible=False)
marker1.penup()
marker1.forward(65)
marker1.color('grey')
marker1.write('', align='center', font=FONT)
marker2 = Turtle(visible=False)
marker2.penup()
marker2.backward(85)
marker2.color('grey')
marker2.write('', align='center', font=FONT)
marker3 = Turtle(visible=False)
marker3.penup()
marker3.goto(0, 220)
marker3.color('grey')
marker3.write('', align='center', font=FONT)
screen.tracer(False)
second_hand, minute_hand, hour_hand = setup()
button = Turtle('circle')
button.shapesize(BUTTON_SIZE / CURSOR_SIZE, outline=2)
button.color('black', 'red')
button.penup()
button.goto(250, 250) # move the button into position
marker4 = Turtle(visible=False)
marker4.penup()
marker4.goto(button.xcor(), button.ycor() - BUTTON_SIZE/2 - FONT_SIZE - 2)
marker4.write('Set Alarm', align='center', font=FONT)
screen.update()
alarm_clock_dic = []
button.onclick(set_alarm)
tick()
screen.mainloop()