1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 导入tkinter库,并设置别名为tk
import tkinter as tk
import time
from datetime import datetime

# 创建Tk对象,Tk代表窗口
root = tk.Tk()

# 设置窗口大小
winWidth = 560
winHeight = 160
# 获取屏幕分辨率
screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()

x = int((screenWidth - winWidth) / 2)
y = int((screenHeight - winHeight) / 2)
# 设置窗口初始位置在屏幕居中
root.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))

var = tk.StringVar()
var2 = tk.StringVar()

def gettime():
var.set(time.strftime("%Y-%m-%d %X")) # 获取当前时间
root.after(1000, gettime) # 每隔1s调用函数 gettime 自身获取时间

def week():
week_list = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
a = week_list[datetime.now().weekday()]
return a

var.set(gettime())
var2.set(week())

# 创建Label对象,第一个参数指定该Label放入root
w1 = tk.Label(root, textvariable=var, font=("微软雅黑", 40),width=16,height=1)
w1.place(x=0,y=0)
w2 = tk.Label(root, textvariable=var2, font=("微软雅黑", 40),width=5,height=1)
w2.place(x=0,y=1)

# 调用pack进行布局
w1.pack()
w2.pack()

root.overrideredirect(1) # 去除窗口边框
root.wm_attributes("-alpha", 0.5) # 透明度(0.0~1.0)
root.wm_attributes("-toolwindow", True) # 置为工具窗口(没有最大最小按钮)
root.wm_attributes("-topmost", True) # 永远处于顶层

def myquit(*args):
root.destroy()

root.bind("<Any-KeyPress>", myquit)

def StartMove(event):
global x, y
x = event.x
y = event.y

def StopMove(event):
global x, y
x = None
y = None

def OnMotion(event):
global x, y
deltax = event.x - x
deltay = event.y - y
root.geometry("+%s+%s" % (root.winfo_x() + deltax, root.winfo_y() + deltay))
root.update()
print(event.x, event.y, root.winfo_x(), root.winfo_y(), root.winfo_width(), root.winfo_height())


root.bind("<ButtonPress-1>", StartMove) # 监听左键按下操作响应函数
root.bind("<ButtonRelease-1>", StopMove) # 监听左键松开操作响应函数
root.bind("<B1-Motion>", OnMotion) # 监听鼠标移动操作响应函数

# 启动主窗口的消息循环
root.mainloop()