Tkinter制作工具学习记录(一)界面

参考文章:tkinter创建GUI实现多界面切换以及python中处理多个异常不退出程序_我要变秃头的博客-CSDN博客_python tkinter 多界面在python中使用tkinter模块实现多界面切换,并在python中处理多个异常不退出程序https://blog.csdn.net/m0_47732121/article/details/123977459

 效果:

 1.顶部

 

1.1设置窗口居中

    def tk_Size(win,t,ww,wh,xr,yr):
        win.title(t)
        sw = win.winfo_screenwidth()
        sh = win.winfo_screenheight()
        x = (sw - ww) / xr
        y = (sh - wh) / yr
        win.geometry("%dx%d+%d+%d" % (ww, wh, x, y))
        win.resizable(0, 0)
        win.resizable(0, 0)

1.2设置窗体title图片

from PIL import Image, ImageTk

class window():
    def __init__(self):
        self.window = tk.Tk()
        method.tk_Size(win=self.window, ww=1080, wh=680, xr=2, yr=10, t='Gingerlite工具-v1.0.0.1-demo')
        tk.Label(self.window.iconbitmap("images/data.ico"))

1.3插入图片

        #打开图片
        ico1 = Image.open("images/lite.jpg")
        #设置图片大小
        ico1 = ico1.resize((100, 100))
        ico = ImageTk.PhotoImage(ico1)
        labelimage = tk.Label(frame0, image=ico)
        labelimage.place(x=0, y=0)

1.4设置label

labelmcsip = tk.Label(frame0, text='MSCIP:', font=("微软雅黑", '12'), bg='lightgreen')
        labelmcsip.place(relx=0.1, rely=0.2, width=80, height=32)
        EntryMSCIP = tk.Entry(frame0, insertbackground='deepskyblue', insertwidth='6', foreground='seagreen', font=("微软雅黑", '14'))

参考:

Tkinter 组件详解(一):Label_来自江南的你的博客-CSDN博客_tk.labelicon-default.png?t=M3K6https://blog.csdn.net/qq_41556318/article/details/85079411

1.5python颜色代码表

参考:

python 颜色代码_Ramiro Luo的博客-CSDN博客_python颜色代码表'aliceblue': '#F0F8FF','antiquewhite': '#FAEBD7','aqua': '#00FFFF','aquamarine': '#7FFFD4','azure': '#F0FFFF','beige': '#F5F5DC','bisque': '#FFE4C4','black': https://blog.csdn.net/qq_37844142/article/details/108282190

1.6实时显示时间

self.window.after(100, self.timeUpdate)
#时钟
    def timeUpdate(self):
        self.time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        labelInfo.config(text="时间:" + self.time)
        self.window.after(100, self.timeUpdate)  # 每隔一秒,时间变化一次

1.7获取本机IP

#获取本机IP
    def tk_host_ip(slef):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(('8.8.8.8', 80))
            ip = s.getsockname()[0]
        finally:
            s.close()
        return ip

1.8输入内容判断是否为ip格式

    #IP格式判断
    def tk_Check_Ip(v):
        p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
        if p.match(v):
            return 1
        else:
            return 0

1.9按钮【连接】,错误弹窗设置

网络检查:

    # 网络检查
    def tk_Check_Net(self):
        ip = EntryMSCIP.get()
        m_ping = os.system('ping %s -n 2 -w 1' % (ip))
        if m_ping == 0:
            return 1
        else:
            openvpn_status = os.system('tasklist | findstr openvpn-gui.exe')
            if openvpn_status == 0:
                return 0
            else:
                return 2

弹窗设置:

#连接
    def connect(self):
        mcsip = EntryMSCIP.get()
        key1 = method.tk_Check_Ip(v=mcsip)
        if key1 == 0:
            messagebox.showerror('提示', '输入的IP格式不正确,请重新输入')
            EntryMSCIP.delete(0, "end")
        else:
            key2 = method.tk_Check_Net(self=self)
            if key2 == 1:
                print('1')
                Radiobuttonfalse.config(bg='grey')
                Radiobuttonsucess.config(bg='lightgreen')
            elif key2 == 2:
                messagebox.showinfo('提示', '未检测到vpn,请检查')
                Radiobuttonfalse.config(bg='tomato')
                Radiobuttonsucess.config(bg='grey')
            elif key2 == 0:
                messagebox.showerror('提示', '网络未能正常连接,请检查')
                Radiobuttonfalse.config(bg='tomato')
                Radiobuttonsucess.config(bg='grey')

网络成功效果:

失败效果:

 

 

 1.11窗口切换

# 界面切换按钮
        btn0 = tk.Button(frame0, text='主页-未开放',state='disable', command=lambda: self.create_framemain())
        btn0.place(relx=0, rely=0.7, relwidth=0.125)

        btn1 = tk.Button(frame0, text='ccu', command=lambda: MyThread(self.create_frameccu))
        btn1.place(relx=0.125, rely=0.7, relwidth=0.25)

        btn2 = tk.Button(frame0, text='RCU-未开放',  state='disable', command=lambda: self.create_framercu())
        btn2.place(relx=0.375, rely=0.7, relwidth=0.25)

        btn3 = tk.Button(frame0, text='留言-未开放', state='disable', command=lambda: self.create_framemessage())
        btn3.place(relx=0.625, rely=0.7, relwidth=0.25)

        btn4 = tk.Button(frame0, text='退出', command=lambda: (exit()))
        btn4.place(relx=0.875, rely=0.7, relwidth=0.125)

2.ccu窗口

#ccu窗口
    def create_frameccu(self):
        global framemessage, framercu, frameccu, framemain, EntryDir
        try:
            frameccu.destroy()
        except:
            pass
        finally:
            try:
                framercu.destroy()
            except:
                pass
            finally:
                try:
                    framemessage.destroy()
                except:
                    pass
                finally:
                    try:
                        framemain.destroy()
                    except:
                        pass
                    finally:
                        frameccu = tk.Frame(self.window, height=600, width=1080)
                        frameccu.place(relx=0, rely=0.14)
                        frameccu.pack_propagate(0)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值