换行 in window and linux

本文介绍了在Linux和Windows平台上处理文本文件时遇到的换行符差异问题。具体来说,Linux使用/n作为换行符,而Windows则使用/r/n。当文件从Windows复制到Linux时,需要使用sed命令删除额外的/r字符。

文本数据linux和windows处理换行符差一个字符.
linux是/n
windows是/r/n

 

If copy file created in window to linux, you need the delete /r .

 

sed -i 's//r//g'  file-name

 

文件file-name里的/r没了

import PySimpleGUI as Py import time import os image_path3 = "D:/software/demo/pythonProject/images/image3.png" image_path4 = "D:/software/demo/pythonProject/images/image4.png" image_path5 = "D:/software/demo/pythonProject/images/image5.png" image_path6 = "D:/software/demo/pythonProject/images/image6.png" image_path7 = "D:/software/demo/pythonProject/images/image7.png" # 检查图片是否存在 if not os.path.exists(image_path3): print(f"[!] 图片3不存在: {image_path3}") image_path3 = None if not os.path.exists(image_path4): print(f"[!] 图片4不存在: {image_path4}") image_path4 = None if not os.path.exists(image_path5): print(f"[!] 图片5不存在: {image_path5}") image_path5 = None if not os.path.exists(image_path6): print(f"[!] 图片6不存在: {image_path6}") image_path6 = None if not os.path.exists(image_path7): print(f"[!] 图片7不存在: {image_path7}") image_path7 = None b = 'zbjszj' layout1 = [[Py.Text('jdsq---')], [Py.Button('tips:zb'), Py.Button('wykd')]] window_position = (435, 0) # 定义主窗口的布局,这里设置了窗口在屏幕上的起始位置为(横坐标50, 纵坐标50) window1 = Py.Window('jdsq', layout1, size=(1665, 68), no_titlebar=True, location=window_position, finalize=True) window1.keep_on_top_set() Py.theme('DarkBlue3') # PySimpleGUI 设置主题颜色 # 全局标志防止重复运行 reminder_running = False def run_periodic_reminder(): global reminder_running if reminder_running: print("[!] 提醒功能已在运行,忽略重复调用") return reminder_running = True layout7 = [[Py.Image(key='img7', filename=image_path7) if image_path7 else Py.Text("加载失败")], [Py.Text("txdj")], [Py.Text("📌 周期性提醒已激活:每次取消后10秒自动重试")], [Py.Multiline("", size=(60, 4), key='-OUTPUT-', disabled=True, autoscroll=True)], [Py.Button('yzxpc', key='EXIT')]] window7 = Py.Window('jbdz:cc', layout7, size=(475, 480), finalize=True) next_popup_time = time.time() + 1 reminder_active = True popup_active = False input_popup = None def show_input_popup(): layout_popup = [[Py.Text('mbwbyd')], [Py.Input(key='-INPUT-', size=(15, 1), focus=True)], [Py.Button('fsydlc', bind_return_key=True), Py.Button('bsyyd')]] return Py.Window('xkdz', layout_popup, keep_on_top=True, finalize=True) try: while True: # === 安全 read():用 try-except 捕获 TclError === try: event7, values7 = window7.read(timeout=100) except (RuntimeError, Exception) as e: # 常见于窗口关闭后底层 TK 资源已释放 if "wrapped C/C++ object has been deleted" in str(e) or "TclError" in str(e): print("[INFO] 窗口已被系统关闭,yzxpc提醒循环。") break else: print(f"[ERROR] Unexpected error: {e}") break if event7 in (None, 'EXIT', Py.WINDOW_CLOSED): # 如果 read 返回 None,说明窗口被关闭 break now = time.time() # 自动弹出输入框逻辑 if reminder_active and not popup_active and next_popup_time and now >= next_popup_time: popup_active = True try: input_popup = show_input_popup() except Exception as e: print(f"[Error] 无法创建弹窗: {e}") popup_active = False next_popup_time = now + 10 continue if input_popup: # 处理输入弹窗 try: event_p, values_p = input_popup.read(timeout=100) except (RuntimeError, Exception): input_popup = None popup_active = False continue if event_p == 'fsydlc': user_input = values_p['-INPUT-'].strip() try: num = float(user_input) formatted_num = int(num) if num.is_integer() else num window7['-OUTPUT-'].update(f"✔️ 录入成功:{formatted_num} —— 提醒停止\n", append=True) reminder_active = False next_popup_time = None except ValueError: window7['-OUTPUT-'].update(f"❌ 输入无效:'{user_input}',10秒后重试...\n", append=True) next_popup_time = time.time() + 10 finally: try: input_popup.close() except: pass input_popup = None popup_active = False elif event_p in (None, 'bsyyd', Py.WINDOW_CLOSED): try: input_popup.close() except: pass input_popup = None popup_active = False window7['-OUTPUT-'].update("⛔ 用户取消,10秒后将再次提醒...\n", append=True) next_popup_time = time.time() + 10 finally: # 确保清理 if 'window7' in locals() or window7 is not None: try: window7.close() except: pass if input_popup is not None: try: input_popup.close() except: pass reminder_running = False 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='img3', filename=image_path3) if image_path3 else Py.Text("加载失败")], [Py.Text("rmxglx")], [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=(590, 10), key='-HISTORY-', relief='sunken', background_color='white', text_color='black', font=('Courier', 10))]], size=(590, 50), pad=10)], # 长度约600 字符,超出则自动换行,最多10行,控制整体显示区域大小以便触发换行 [Py.Button('mphcxxl'), Py.Button('jchdj')], # 用一个大括号使他们在同一行显示 [Py.Button('txlxk'), Py.Button('bfhycg')]] window3 = Py.Window('jbdz:pddb', layout3, size=(630, 565), 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, 'bfhycg'): window3.close() elif event3 == 'mphcxxl': window1.hide() elif event3 == 'txlxk': window3.close() layout4 = [[Py.Image(key='img4', filename=image_path4) if image_path4 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=(410, 10), key='-HISTORY-', relief='sunken', background_color='white', text_color='black', font=('Courier', 10))]], size=(410, 50), pad=10)], # 控制整体显示区域大小以便触发换行 [Py.Button('jrrsby'), Py.Button('jrqsby')]] window4 = Py.Window('jbdz:pdbyqr', layout4, size=(450, 440), 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': window4.close() Py.theme('LightBlue') recorded_numbers = [] # 历史记录列表 layout5 = [[Py.Image(key='img5', filename=image_path5) if image_path5 else Py.Text("加载失败")], [Py.Text("请输入当前数字:")], [Py.Input(key='-INPUT-', size=(30, 1), do_not_clear=False)], # do_not_clear=False 表示默认清空 [Py.Button('cxzjqpqh', bind_return_key=True), Py.Button('bsyjcxh')], [Py.HorizontalSeparator()], [Py.Text("已保留的数字(横向排列,自动换行):")], [Py.Text("", size=(50, 2), key='-HISTORY-', relief='sunken', background_color='white', text_color='black')]] window5 = Py.Window('jbdz:rsby', layout5) while True: event5, values5 = window5.read() if event5 == Py.WINDOW_CLOSED: break input_value = values5['-INPUT-'].strip() if event5 == 'cxzjqpqh': window5.close() run_periodic_reminder() 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) window5['-INPUT-'].update('') # 清空输入框(do_not_clear=False 已自动处理,但仍显式更新以防万一) except ValueError: window5['-STATUS-'].update("❌ 不是有效数字!") elif event5 == 'bsyjcxh': window5['-INPUT-'].update('') # 直接清空输入框 elif event4 == 'jrqsby': window4.close() Py.theme('LightBlue') recorded_numbers = [] # 历史记录列表 layout6 = [[Py.Image(key='img6', filename=image_path6) if image_path6 else Py.Text("加载失败")], [Py.Text("请输入当前数字:")], [Py.Input(key='-INPUT-', size=(30, 1), do_not_clear=False)], # do_not_clear=False 表示默认清空 [Py.Button('cxzcpb', bind_return_key=True), Py.Button('bsyjcxh')], [Py.HorizontalSeparator()], [Py.Text("已保留的数字(横向排列,自动换行):")], [Py.Text("", size=(50, 2), key='-HISTORY-', relief='sunken', background_color='white', text_color='black')]] window6 = Py.Window('jbdz:qsby', layout6) while True: event6, values6 = window6.read() if event6 == Py.WINDOW_CLOSED: break input_value = values6['-INPUT-'].strip() if event6 == 'cxzcpb': window6.close() run_periodic_reminder() 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) window6['-INPUT-'].update('') # 清空输入框(do_not_clear=False 已自动处理,但仍显式更新以防万一) except ValueError: window6['-STATUS-'].update("❌ 不是有效数字!") elif event6 == 'bsyjcxh': # 直接清空输入框 window6['-INPUT-'].update('') elif event3 == 'jchdj': window3.close() window1.un_hide() run_periodic_reminder() # 弹出礼物窗口 while True: # 主事件循环 event1, values1 = window1.read() if event1 == Py.WIN_CLOSED: break if event1 == 'tips:zb': layout2 = [[Py.Text(b, 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() 代码所有窗口均置顶
11-10
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值