需求
自己用PySimpleGUI写了个小工具,但是发现它的spin不支持循环。
Tkinter本身的Spinbox有wrap这个开关可以觉得是否支持循环,但是没看到PySimpleGUI也支持这个特性。
代码实现
所谓spin的循环,是指当值变换到最大最小值时,可以在最大最小值直接切换。
下面我是为spin增加循环实现的代码:
import PySimpleGUI as psg
from datetime import datetime
import os
import shutil
#选择窗口主题
psg.theme("Dark2")
# 放大方向
zoom_directions = ["变大", "变小"]
zoom_dir = psg.Spin(zoom_directions, readonly=True, size=5,
enable_events=True, key='-ZOOM DIR-')
# 放大倍数
zoom_times = ["0.7X", "1X", "2.5X"]
zoom_time = psg.Spin(zoom_times, readonly=True, size=5,
enable_events=True, key='-ZOOM TIME-')
layout = [
[psg.Text('变倍方向:'), zoom_dir, psg.Text(" 倍率:"), zoom_time],
[psg.Button('Exit', size=(10, 1))]
]
window = psg.Window('GUI测试', layout, font='_ 18', size=(500, 120))
last_zoom_time = None
last_dir = None
while True:
event, values = window.read()
if event == 'Exit' or event == psg.WIN_CLOSED:
break
print(f"event:{event}")
if event == "-ZOOM DIR-":
if last_dir == values['-ZOOM DIR-']:
if values['-ZOOM DIR-'] == zoom_directions[0]:
window['-ZOOM DIR-'].update(zoom_directions[1])
else:
window['-ZOOM DIR-'].update(zoom_directions[0])
# print(f" direction:{window['-ZOOM DIR-'].get()}")
elif event == "-ZOOM TIME-":
if last_zoom_time == values['-ZOOM TIME-']:
if values['-ZOOM TIME-'] == zoom_times[0]:
window['-ZOOM TIME-'].update(zoom_times[2])
else:
window['-ZOOM TIME-'].update(zoom_times[0])
# print(f" times:{window['-ZOOM TIME-'].get()}")
last_dir = window['-ZOOM DIR-'].get()
last_zoom_time = window['-ZOOM TIME-'].get()
window.close()
代码效果:
当倍率变到最大,继续点击向上的按钮,则会变为最小值。