You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

利用CtkSwitch切换Ping命令:实现单次/持续Ping功能需求

用Customtkinter Switch组件切换Ping命令实现方案

需求概述

通过CtkSwitch组件切换Ping行为:

  • 开关默认关闭时,执行单次Ping命令:ping {hostname}
  • 开关开启时,执行持续Ping命令:ping -t {hostname}

修改后的完整代码

import customtkinter
import os

customtkinter.set_appearance_mode("Dark")
customtkinter.set_default_color_theme("blue")


class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        # 窗口配置
        self.title("STEVE's IRT TOOL")
        self.geometry(f"{1100}x{580}")
        self.resizable(False, False)
        self.iconbitmap('icon.ico')

        # 网格布局配置
        self.grid_columnconfigure(1, weight=1)
        self.grid_columnconfigure((2, 3), weight=0)
        self.grid_rowconfigure((0, 1, 2), weight=1)

        # 侧边栏框架及组件
        self.sidebar_frame = customtkinter.CTkFrame(self, width=140, corner_radius=0)
        self.sidebar_frame.grid(row=0, column=0, rowspan=4, sticky="nsew")
        self.sidebar_frame.grid_rowconfigure(4, weight=1)

        self.logo_label = customtkinter.CTkLabel(self.sidebar_frame, text="IRT Tool", font=customtkinter.CTkFont(size=20, weight="bold"))
        self.logo_label.grid(row=0, column=0, padx=20, pady=(20, 10))

        self.info = customtkinter.CTkEntry(self.sidebar_frame, placeholder_text="Enter Input")
        self.info.grid(row=1, column=0, padx=20, pady=10)

        self.sidebar_button_ping = customtkinter.CTkButton(self.sidebar_frame, text="Ping", fg_color="transparent", border_width=2, text_color=("gray10", "#DCE4EE"), command=self.button_ping)
        self.sidebar_button_ping.grid(row=2, column=0, padx=20, pady=10)

        self.switch_var = customtkinter.StringVar(value="off")
        self.switch = customtkinter.CTkSwitch(self.sidebar_frame, text="Constant", variable=self.switch_var, onvalue="on", offvalue="off")
        self.switch.grid(row=3, column=0, padx=20, pady=10)

        # 外观模式设置
        self.appearance_mode_label = customtkinter.CTkLabel(self.sidebar_frame, text="Appearance Mode:", anchor="w")
        self.appearance_mode_label.grid(row=9, column=0, padx=20, pady=(10, 0))
        self.appearance_mode_optionemenu = customtkinter.CTkOptionMenu(self.sidebar_frame, values=["Light", "Dark", "System"], command=self.change_appearance_mode_event)
        self.appearance_mode_optionemenu.grid(row=10, column=0, padx=20, pady=(10, 10))

        # 输出窗口
        self.print_out = customtkinter.CTkTextbox(self, width=250)
        self.print_out.grid(row=0, rowspan=2, column=1, padx=(20, 0), pady=(20, 0), sticky="nsew")

    def read_output(self, fp):
        line = fp.readline()
        if line:
            self.print_out.insert("end", line)
            self.print_out.see("end")
            self.print_out.after(10, self.read_output, fp)

    def button_ping(self):
        hostname = self.info.get()
        # 根据开关状态构造Ping命令
        if self.switch_var.get() == "on":
            cmd = f"ping -t {hostname}"
        else:
            cmd = f"ping {hostname}"
        fp = os.popen(cmd)
        self.read_output(fp)

    def change_appearance_mode_event(self, new_appearance_mode: str):
        customtkinter.set_appearance_mode(new_appearance_mode)

    def sidebar_button_event(self):
        print("sidebar_button click")


if __name__ == "__main__":
    app = App()
    app.mainloop()

关键改动说明

  • 移除全局变量:将info、print_out、switch_var改为实例变量(self.xxx),避免全局变量带来的作用域问题,代码更规范。
  • 简化Switch逻辑:不需要单独的switch_event函数,直接在button_ping中读取switch_var的状态来构造命令,减少冗余代码。
  • 修正命令参数:关闭状态下直接执行默认单次Ping,不再错误使用-r参数;开启状态下添加-t参数实现持续Ping。
  • 调整内部函数为实例方法:将read_output和button_ping改为类的实例方法,确保能正确访问实例变量。

内容的提问来源于stack exchange,提问作者Steve

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 23:54:54