python实现一个浮动窗口随机显示四级单词在桌面跑来跑去
实现一个浮动窗体随机显示四级单词在windows桌面置顶移动
tkinter
库来创建窗口和显示单词,以及random
库来随机选择单词。 使用after
方法来定时更新窗口的位置,实现单词窗口的慢慢移动效果
使用pandas读取Excel文件,并将每行的单词和释义对应关系存储在一个字典中
实现鼠标悬停时显示对应的解释,或者直接显示对应解释FloatingWordWindow
类中添加一个标签来显示解释,并在鼠标进入标签时更新解释内容
鼠标悬停的时候显示对应的解释,也就是对应键的值 不悬停的时候,不显示解释,返回显示随机单词 wraplength是自动换行
使用虚拟环境执行 E:\pycharm\anaconda\python.exe vocabulary.py
后台执行 E:\pycharm\anaconda\python.exe vocabulary.py & win不可用 在Linux或Mac系统下,你可以使用&
符号
在Windows系统下,你可以使用start命令来启动一个新的窗口并在其中运行脚本,然后关闭该窗口,脚本将在后台继续执行。例如:
start /B E:\pycharm\anaconda\python.exe vocabulary.py 也不行
powershell可以
cd "E:\pythonProject\other"
Start-Process -FilePath "E:\pycharm\anaconda\python.exe" -ArgumentList "vocabulary.py" -WindowStyle Hidden
初始版本
import tkinter as tk
import random
import pandas as pd
class FloatingWordWindow:
def __init__(self, master):
self.master = master
self.master.overrideredirect(True) # 隐藏标题栏和边框
self.master.attributes('-topmost', True) # 置顶窗口
self.master.attributes('-alpha', 0.7) # 设置透明度
self.master.geometry('200x50+100+100') # 初始位置和大小
self.word_label = tk.Label(self.master, font=('Arial', 20))
self.word_label.pack(expand=True)
self.move_window()
def move_window(self):
x = random.randint(0, self.master.winfo_screenwidth() - self.master.winfo_width())
y = random.randint(0, self.master.winfo_screenheight() - self.master.winfo_height())
self.master.geometry(f'+{x}+{y}')
self.master.after(3000, self.move_window) # 每隔3秒更新一次位置
def update_word(self):
words = ['apple', 'banana', 'orange', 'grape', 'watermelon'] # 替换为你的四级单词列表
df=pd.read_excel("大学英语四级词汇完整版带音标-顺序版.xlsx")
# 提取单词和释义列的内容
word_column = df.columns[0] # 第一列是单词列
meaning_column = df.columns[1] # 第二列是释义列
# 创建一个字典,将单词和释义对应关系存储起来
word_meaning_dict = dict(zip(df[word_column], df[meaning_column]))
# 将字典的键转换为列表
words_list = list(word_meaning_dict.keys())
# 随机选择一个单词
random_word = random.choice(words_list)
self.word_label.config(text=random_word)
# 绑定鼠标悬停事件
self.word_label.bind("<Enter>",
lambda event, word=random_word: self.show_meaning(event, word, word_meaning_dict))
self.word_label.bind("<Leave>", lambda event: self.hide_meaning(random_word)) # 绑定鼠标移出事件
self.master.after(3000, self.update_word) # 每隔3秒更新一次单词
def show_meaning(self, event, word, word_meaning_dict):
meaning = word_meaning_dict.get(word, "Meaning not found")
self.word_label.config(text=f"{meaning}",font=('Arial', 10),wraplength=150)
def hide_meaning(self,random_word):
self.word_label.config(text=random_word,font=('Arial', 20),wraplength=150) # 清空文本
def main():
root = tk.Tk()
root.attributes('-toolwindow', True) # 隐藏任务栏图标
root.attributes('-alpha', 0.7) # 设置透明度
root.geometry('200x50+150+150') # 初始位置和大小
app = FloatingWordWindow(root)
app.update_word()
root.mainloop()
if __name__ == '__main__':
main()
优化这个代码让excel读取只运行一次
import tkinter as tk
import random
import pandas as pd
class FloatingWordWindow:
def __init__(self, master):
self.master = master
self.master.overrideredirect(True) # 隐藏标题栏和边框
self.master.attributes('-topmost', True) # 置顶窗口
self.master.attributes('-alpha', 0.7) # 设置透明度
self.master.geometry('200x50+100+100') # 初始位置和大小
self.word_label = tk.Label(self.master, font=('Arial', 20))
self.word_label.pack(expand=True)
self.load_word_meanings() # 加载单词和释义
self.move_window()
def load_word_meanings(self):
df = pd.read_excel("大学英语四级词汇完整版带音标-顺序版.xlsx")
self.word_meaning_dict = dict(zip(df[df.columns[0]], df[df.columns[1]])) # 单词和释义对应的字典
def move_window(self):
x = random.randint(0, self.master.winfo_screenwidth() - self.master.winfo_width())
y = random.randint(0, self.master.winfo_screenheight() - self.master.winfo_height())
self.master.geometry(f'+{x}+{y}')
self.master.after(3000, self.move_window) # 每隔3秒更新一次位置
def update_word(self):
random_word = random.choice(list(self.word_meaning_dict.keys())) # 随机选择一个单词
self.word_label.config(text=random_word)
# 绑定鼠标悬停事件
self.word_label.bind("<Enter>",
lambda event, word=random_word: self.show_meaning(event, word))
self.word_label.bind("<Leave>", lambda event: self.hide_meaning(random_word)) # 绑定鼠标移出事件
self.master.after(3000, self.update_word) # 每隔3秒更新一次单词
def show_meaning(self, event, word):
meaning = self.word_meaning_dict.get(word, "Meaning not found")
self.word_label.config(text=f"{meaning}", font=('Arial', 10), wraplength=150)
def hide_meaning(self, word):
self.word_label.config(text=word, font=('Arial', 20), wraplength=150) # 清空文本
def main():
root = tk.Tk()
root.attributes('-toolwindow', True) # 隐藏任务栏图标
root.attributes('-alpha', 0.7) # 设置透明度
root.geometry('200x50+150+150') # 初始位置和大小
app = FloatingWordWindow(root)
app.update_word()
root.mainloop()
if __name__ == '__main__':
main()
优化二 悬停显示
import tkinter as tk
import random
import pandas as pd
class FloatingWordWindow:
def __init__(self, master):
self.master = master
self.master.overrideredirect(True) # 隐藏标题栏和边框
self.master.attributes('-topmost', True) # 置顶窗口
self.master.attributes('-alpha', 0.7) # 设置透明度
self.master.geometry('200x50+100+100') # 初始位置和大小
self.word_label = tk.Label(self.master, font=('Arial', 20))
self.word_label.pack(expand=True)
self.move_window()
# 读取 Excel 数据
self.load_excel_data()
def move_window(self):
x = random.randint(0, self.master.winfo_screenwidth() - self.master.winfo_width())
y = random.randint(0, self.master.winfo_screenheight() - self.master.winfo_height())
self.master.geometry(f'+{x}+{y}')
self.master.after(3000, self.move_window) # 每隔3秒更新一次位置
def load_excel_data(self):
self.df = pd.read_excel("大学英语四级词汇完整版带音标-顺序版.xlsx")
self.word_column = self.df.columns[0] # 第一列是单词列
self.meaning_column = self.df.columns[1] # 第二列是释义列
# 创建一个字典,将单词和释义对应关系存储起来
self.word_meaning_dict = dict(zip(self.df[self.word_column], self.df[self.meaning_column]))
# 将字典的键转换为列表
self.words_list = list(self.word_meaning_dict.keys())
def update_word(self):
# 随机选择一个单词
random_word = random.choice(self.words_list)
self.word_label.config(text=random_word)
# 绑定鼠标悬停事件
self.word_label.bind("<Enter>",
lambda event, word=random_word: self.show_meaning(event, word))
self.word_label.bind("<Leave>", lambda event: self.hide_meaning(random_word)) # 绑定鼠标移出事件
self.master.after(3000, self.update_word) # 每隔3秒更新一次单词
def show_meaning(self, event, word):
meaning = self.word_meaning_dict.get(word, "Meaning not found")
self.word_label.config(text=f"{meaning}", font=('Arial', 10), wraplength=150)
def hide_meaning(self, random_word):
self.word_label.config(text=random_word, font=('Arial', 20), wraplength=150) # 清空文本
def main():
root = tk.Tk()
root.attributes('-toolwindow', True) # 隐藏任务栏图标
root.attributes('-alpha', 0.7) # 设置透明度
root.geometry('200x50+150+150') # 初始位置和大小
app = FloatingWordWindow(root)
app.update_word()
root.mainloop()
if __name__ == '__main__':
main()
优化三 直接显示
import tkinter as tk
import random
import pandas as pd
class FloatingWordWindow:
def __init__(self, master):
self.master = master
self.master.overrideredirect(True) # 隐藏标题栏和边框
self.master.attributes('-topmost', True) # 置顶窗口
self.master.attributes('-alpha', 0.7) # 设置透明度
self.word_label = tk.Label(self.master, font=('Arial', 18))
self.word_label.pack(expand=True, padx=0, pady=0)
self.load_word_meanings() # 加载单词和释义
self.move_window()
def load_word_meanings(self):
df = pd.read_excel("大学英语四级词汇完整版带音标-顺序版.xlsx")
self.word_meaning_dict = dict(zip(df[df.columns[0]], df[df.columns[1]])) # 单词和释义对应的字典
def move_window(self):
x = random.randint(0, self.master.winfo_screenwidth() - self.master.winfo_width())
y = random.randint(0, self.master.winfo_screenheight() - self.master.winfo_height())
self.master.geometry(f'100x25+{x}+{y}') # 初始窗口大小为200x50
self.master.after(3000, self.move_window) # 每隔3秒更新一次位置
def update_word(self):
random_word = random.choice(list(self.word_meaning_dict.keys())) # 随机选择一个单词
meaning = self.word_meaning_dict.get(random_word, "Meaning not found")
# meaning = meaning.replace("\n", "--")
str=random_word+"\n"+meaning;
num_newlines = str.count("\n")
self.word_label.config(text=random_word+"\n"+meaning,justify="left",padx=0, pady=0)
self.word_label.config(height=(num_newlines+1)*25) # Set height based on number of newlines
# 根据单词长度和字体大小调整窗口大小
word_length = len(random_word+meaning)
font_size = 18
window_width = max(18, word_length * font_size)
self.master.geometry(f'{window_width}x{(num_newlines+1)*25}') # 调整窗口宽度
# # 绑定鼠标悬停事件
# self.master.bind("<Enter>",
# lambda event, word=random_word: self.show_meaning(event, word))
# self.master.bind("<Leave>", lambda event: self.hide_meaning(random_word)) # 绑定鼠标移出事件
# # 绑定左键单击事件
self.master.bind("<Button-1>", lambda event: self.update_word())
self.master.after(3000, self.update_word) # 每隔3秒更新一次单词
def show_meaning(self, event, word):
meaning = self.word_meaning_dict.get(word, "Meaning not found")
meaning = meaning.replace("\n", "--")
self.word_label.config(text=f"{meaning}", font=('Arial', 10),padx=0, pady=0)
word_length = len(meaning)
font_size = 20
window_width = max(20, word_length * font_size)
self.master.geometry(f'{window_width}x30') # 调整窗口宽度
def hide_meaning(self, word):
self.word_label.config(text=word, font=('Arial', 20), wraplength=150,padx=0, pady=0) # 清空文本
def main():
root = tk.Tk()
root.attributes('-toolwindow', True) # 隐藏任务栏图标
root.attributes('-alpha', 0.7) # 设置透明度
app = FloatingWordWindow(root)
app.update_word()
root.mainloop()
if __name__ == '__main__':
main()
优化四
随机不重复
python
import tkinter as tk
import random
import pandas as pd
class FloatingWordWindow:
def __init__(self, master):
self.master = master
self.master.overrideredirect(True) # 隐藏标题栏和边框
self.master.attributes('-topmost', True) # 置顶窗口
self.master.attributes('-alpha', 0.7) # 设置透明度
self.word_label = tk.Label(self.master, font=('Arial', 20))
self.word_label.pack(expand=True, padx=0, pady=0)
self.load_word_meanings() # 加载单词和释义
self.move_window()
def load_word_meanings(self):
df = pd.read_excel("大学英语四级词汇完整版带音标-顺序版.xlsx")
self.word_meaning_dict = dict(zip(df[df.columns[0]], df[df.columns[1]])) # 单词和释义对应的字典
self.available_words = list(self.word_meaning_dict.keys()) # 可选择的单词列表
def move_window(self):
x = random.randint(0, self.master.winfo_screenwidth() - self.master.winfo_width())
y = random.randint(0, self.master.winfo_screenheight() - self.master.winfo_height())
self.master.geometry(f'100x30+{x}+{y}') # 初始窗口大小为200x50
self.master.after(3000, self.move_window) # 每隔3秒更新一次位置
def update_word(self):
if not self.available_words: # 如果可选择的单词列表为空,则重新加载
self.available_words = list(self.word_meaning_dict.keys())
random_word = random.choice(self.available_words) # 从可选择的单词列表中随机选择一个单词
self.available_words.remove(random_word) # 从可选择的单词列表中删除已选择的单词
self.word_label.config(text=random_word,padx=0, pady=0)
# 根据单词长度和字体大小调整窗口大小
word_length = len(random_word)
font_size = 20
window_width = max(20, word_length * font_size)
self.master.geometry(f'{window_width}x30') # 调整窗口宽度
# 绑定鼠标悬停事件
self.master.bind("<Enter>",
lambda event, word=random_word: self.show_meaning(event, word))
self.master.bind("<Leave>", lambda event: self.hide_meaning(random_word)) # 绑定鼠标移出事件
# # 绑定左键单击事件
self.master.bind("<Button-1>", lambda event: self.update_word())
self.master.after(3000, self.update_word) # 每隔3秒更新一次单词
def show_meaning(self, event, word):
meaning = self.word_meaning_dict.get(word, "Meaning not found")
meaning = meaning.replace("\n", "--")
word_length = len(meaning)
font_size = 11
window_width = max(10, word_length * font_size)
self.master.geometry(f'{window_width}x30') # 调整窗口宽度
self.word_label.config(text=f"{meaning}", font=('Arial', 10), justify="left", wraplength=999, padx=0, pady=0)
def hide_meaning(self, word):
self.word_label.config(text=word, font=('Arial', 20), wraplength=150,padx=0, pady=0) # 清空文本
def main():
root = tk.Tk()
root.attributes('-toolwindow', True) # 隐藏任务栏图标
root.attributes('-alpha', 0.7) # 设置透明度
app = FloatingWordWindow(root)
app.update_word()
root.mainloop()
if __name__ == '__main__':
main()