import PySimpleGUI as Py
import time
import threading
import os
import queue
image_path1 = "D:/software/demo/pythonProject/images/image1.png"
image_path2 = "D:/software/demo/pythonProject/images/image2.png"
# 检查图片是否存在
if not os.path.exists(image_path1):
print(f"[!] 图片1不存在: {image_path1}")
image_path1 = None
if not os.path.exists(image_path2):
print(f"[!] 图片2不存在: {image_path2}")
image_path2 = None
a = 'jyrz'
g = 'zbjszj'
layout1 = [
[Py.Text('jdsj--')],
[Py.Button('tips'),
Py.Button('wykd')]
]
window_position = (210, 0) # 定义主窗口的布局,这里设置了窗口在屏幕上的起始位置为(横坐标50, 纵坐标50)
window1 = Py.Window('jdsq', layout1, size=(1500, 68), no_titlebar=True, location=window_position, finalize=True)
window1.keep_on_top_set()
Py.theme('DarkBlue3') # PySimpleGUI 设置主题颜色
def run_single_flow():
"""运行一次完整的 window3 流程"""
Py.theme('LightBlue')
input_history = [] # 存储已确认的数字
last_input_time = None
INPUT_TIMEOUT = 5.0
current_text = ''
# 使用一个固定宽度的 Text 框实现横向排列 + 自动换行
layout3 = [
[Py.Image(key='img1', filename=image_path1) if image_path1 else Py.Text("加载失败")],
[Py.Text("请在5秒内输入一个数字,之后5秒无操作将自动记录:")],
[Py.Input(key='-INPUT-', size=(20, 1), focus=True)],
[Py.Text("", size=(60, 1), key='-STATUS-')],
[Py.Text("已记录的数字(横向排列,自动换行):")],
# 设置 size=(None, 10) 表示不限宽度列,高度最多10行;text_color 和 background_color 提升可读性
[Py.Frame('', [[
Py.Text("",
size=(600, 10), # 长度约600字符,超出则自动换行,最多10行
key='-HISTORY-',
relief='sunken',
background_color='white',
text_color='black',
font=('Courier', 10))
]], size=(600, 50), pad=10)], # 控制整体显示区域大小以便触发换行
[Py.Button('cxxldb')],
[Py.Button('txlxk')],
[Py.Button('lxbfh')],
[Py.Button('--jchdj--')],
]
window3 = Py.Window('自动换行横向记录器', layout3, resizable=True)
while True:
event3, values3 = window3.read(timeout=100)
if event3 == Py.WINDOW_CLOSED:
break
new_text = values3['-INPUT-'].strip()
input_changed = new_text != current_text
current_text = new_text
# 验证是否为有效数字
valid_number = False
try:
if current_text:
float(current_text)
valid_number = True
except ValueError:
pass
# 输入变化且合法 → 重置计时器
if input_changed and valid_number:
last_input_time = time.time()
window3['-STATUS-'].update(f"✅ 输入中 '{current_text}' ... 5秒无操作将自动提交")
# 超时自动提交
if last_input_time is not None:
elapsed = time.time() - last_input_time
if elapsed >= INPUT_TIMEOUT:
try:
num = float(current_text)
formatted_num = int(num) if num.is_integer() else num
input_history.append(formatted_num)
# 更新历史显示:空格分隔,让系统自动换行
history_str = ' '.join(map(str, input_history))
window3['-HISTORY-'].update(history_str)
window3['-STATUS-'].update("🎉 已自动记录!")
window3['-INPUT-'].update('')
current_text = ''
last_input_time = None
except Exception as e:
window3['-STATUS-'].update("❌ 提交失败")
last_input_time = None
# 状态栏提示
elif last_input_time is None and current_text and not valid_number:
window3['-STATUS-'].update("❌ 请输入有效的数字")
elif last_input_time and valid_number:
remaining = max(0, int(INPUT_TIMEOUT - (time.time() - last_input_time) + 0.9))
if remaining > 0:
window3['-STATUS-'].update(f"⏳ 还剩 {remaining} 秒自动提交...")
if event3 in (Py.WIN_CLOSED, 'lxbfh'):
break # 正常退出
elif event3 == 'cxxldb':
window1.hide()
elif event3 == 'txlxk':
window3.close()
layout4 = [
[Py.Image(key='img1', filename=image_path1) if image_path1 else Py.Text("加载失败")],
[Py.Text("请在5秒内输入一个数字,之后5秒无操作将自动记录:")],
[Py.Input(key='-INPUT-', size=(20, 1), focus=True)],
[Py.Text("", size=(60, 1), key='-STATUS-')],
[Py.Text("已记录的数字(横向排列,自动换行):")],
# 设置 size=(None, 10) 表示不限宽度列,高度最多10行;text_color 和 background_color 提升可读性
[Py.Frame('', [[
Py.Text("",
size=(600, 10), # 长度约600字符,超出则自动换行,最多10行
key='-HISTORY-',
relief='sunken',
background_color='white',
text_color='black',
font=('Courier', 10))
]], size=(600, 50), pad=10)], # 控制整体显示区域大小以便触发换行
[Py.Button('jrrsby')],
[Py.Button('jrqsby')],
]
window4 = Py.Window('自动换行横向记录器', layout4, resizable=True)
while True:
event4, values4 = window4.read(timeout=100)
if event4 == Py.WINDOW_CLOSED:
break
new_text = values4['-INPUT-'].strip()
input_changed = new_text != current_text
current_text = new_text
# 验证是否为有效数字
valid_number = False
try:
if current_text:
float(current_text)
valid_number = True
except ValueError:
pass
# 输入变化且合法 → 重置计时器
if input_changed and valid_number:
last_input_time = time.time()
window4['-STATUS-'].update(f"✅ 输入中 '{current_text}' ... 5秒无操作将自动提交")
# 超时自动提交
if last_input_time is not None:
elapsed = time.time() - last_input_time
if elapsed >= INPUT_TIMEOUT:
try:
num = float(current_text)
formatted_num = int(num) if num.is_integer() else num
input_history.append(formatted_num)
# 更新历史显示:空格分隔,让系统自动换行
history_str = ' '.join(map(str, input_history))
window4['-HISTORY-'].update(history_str)
window4['-STATUS-'].update("🎉 已自动记录!")
window4['-INPUT-'].update('')
current_text = ''
last_input_time = None
except Exception as e:
window4['-STATUS-'].update("❌ 提交失败")
last_input_time = None
# 状态栏提示
elif last_input_time is None and current_text and not valid_number:
window4['-STATUS-'].update("❌ 请输入有效的数字")
elif last_input_time and valid_number:
remaining = max(0, int(INPUT_TIMEOUT - (time.time() - last_input_time) + 0.9))
if remaining > 0:
window4['-STATUS-'].update(f"⏳ 还剩 {remaining} 秒自动提交...")
elif event4 == 'jrrsby':
Py.theme('LightBlue')
# 历史记录列表
recorded_numbers = []
# 定义布局
layout5 = [
[Py.Text("请输入一个数字:")],
[Py.Input(key='-INPUT-', size=(30, 1), do_not_clear=False)], # do_not_clear=False 表示默认清空
[Py.Button('blkyjc', bind_return_key=True), Py.Button('删除')],
[Py.HorizontalSeparator()],
[Py.Text("已保留的数字(横向排列,自动换行):")],
[Py.Text("", size=(50, 10), key='-HISTORY-', relief='sunken', background_color='white',
text_color='black')]
]
# 创建窗口
window5 = Py.Window('数字记录器 - 保留或删除', layout5)
while True:
event5, values5 = window5.read()
if event5 == Py.WINDOW_CLOSED:
break
input_value = values5['-INPUT-'].strip()
if event5 == 'blkyjc':
timer_queue = queue.Queue()
stop_timer = threading.Event() # 控制是否继续提醒循环
# 主窗口布局
layout7 = [
[Py.Text("已录入的数值记录:")],
[Py.Multiline("", size=(60, 10), key='-OUTPUT-', disabled=True, autoscroll=True)],
[Py.Button('kstx'), Py.Button('yzxpc')]
]
window7 = Py.Window('周期性提醒输入器', layout7)
# 定时器线程:严格每5秒发一次事件(除非被 stop)
def show_gift_popup(q, stop_flag):
while not stop_flag.is_set():
time.sleep(5) # 每隔5秒发送一个弹窗请求
if not stop_flag.is_set(): # 如果未被停止
q.put(('SHOW_POPUP', None))
# 主事件循环
while True:
event7, values7 = window7.read(timeout=100)
if event7 in (Py.WINDOW_CLOSED, 'yzxpc'):
stop_timer.set()
break
if event7 == 'kstx' and not stop_timer.is_set():
stop_timer.clear()
thread = threading.Thread(target=show_gift_popup, args=(timer_queue, stop_timer),
daemon=True)
thread.start()
window7['kstx'].update(disabled=True)
# 处理队列事件
try:
q_event = timer_queue.get_nowait()
if q_event[0] == 'SHOW_POPUP':
user_input = Py.PopupGetText(
'请输入一个数字(OK提交并结束,Cancel则5秒后重试):',
title='提醒输入',
keep_on_top=True
)
if user_input is not None: # 用户点击了 OK
try:
num = float(user_input.strip())
formatted_num = int(num) if num.is_integer() else num
window7['-OUTPUT-'].update(f"✔️ 录入成功:{formatted_num}\n",
append=True)
# ✅ 成功提交 → 停止所有后续提醒
stop_timer.set()
except ValueError:
# 输入无效,不记录,也不停止;继续下一轮提醒
Py.PopupOK("❌ 输入无效,将5秒后重试!", title="警告", icon=None)
# else: 用户点了 Cancel → 什么也不做,定时器将继续运行,5秒后再弹
except queue.Empty:
pass # 无事件则跳过
action = show_gift_popup()
if input_value == '':
window4['-HISTORY-'].update("⚠️ 输入为空,无法保留")
else:
try:
num = float(input_value)
formatted_num = int(num) if num.is_integer() else num
recorded_numbers.append(formatted_num)
# 更新历史显示(横向空格分隔,自动换行)
history_text = ' '.join(map(str, recorded_numbers))
window5['-HISTORY-'].update(history_text)
# 清空输入框(do_not_clear=False 已自动处理,但仍显式更新以防万一)
window5['-INPUT-'].update('')
except ValueError:
window5['-STATUS-'].update("❌ 不是有效数字!")
elif event5 == '删除':
# 直接清空输入框
window5['-INPUT-'].update('')
elif event4 == 'jrqsby':
Py.theme('LightBlue')
# 历史记录列表
recorded_numbers = []
# 定义布局
layout6 = [
[Py.Text("请输入一个数字:")],
[Py.Input(key='-INPUT-', size=(30, 1), do_not_clear=False)], # do_not_clear=False 表示默认清空
[Py.Button('blkyjc', bind_return_key=True), Py.Button('删除')],
[Py.HorizontalSeparator()],
[Py.Text("已保留的数字(横向排列,自动换行):")],
[Py.Text("", size=(50, 10), key='-HISTORY-', relief='sunken', background_color='white',
text_color='black')]
]
# 创建窗口
window6 = Py.Window('数字记录器 - 保留或删除', layout6)
while True:
event6, values6 = window6.read()
if event6 == Py.WINDOW_CLOSED:
break
input_value = values6['-INPUT-'].strip()
if event6 == 'blkyjc':
action = show_gift_popup()
if input_value == '':
window6['-HISTORY-'].update("⚠️ 输入为空,无法保留")
else:
try:
num = float(input_value)
formatted_num = int(num) if num.is_integer() else num
recorded_numbers.append(formatted_num)
# 更新历史显示(横向空格分隔,自动换行)
history_text = ' '.join(map(str, recorded_numbers))
window6['-HISTORY-'].update(history_text)
# 清空输入框(do_not_clear=False 已自动处理,但仍显式更新以防万一)
window6['-INPUT-'].update('')
except ValueError:
window6['-STATUS-'].update("❌ 不是有效数字!")
elif event6 == '删除':
# 直接清空输入框
window6['-INPUT-'].update('')
elif event3 == '--jchdj--':
window3.un_hide()
window1.hide()
# 弹出礼物窗口
action = show_gift_popup()
# 主事件循环
while True:
event1, values1 = window1.read()
if event1 == Py.WIN_CLOSED:
break
if event1 == 'tips':
layout2 = [[Py.Text(g, size=(170, 2), auto_size_text=True)]]
window2 = Py.Window('ktrx', layout2, size=(940, 210), finalize=True, keep_on_top=True)
window2.read(timeout=5000, close=True)
if event1 == 'wykd':
run_single_flow()
最新发布