02.输入界面窗口设置

2020-01-10  本文已影响0人  M_小七

创建一个窗口对象,包含以下内容:
1.请输入IP
2.IP及输入框
3.Port及输入框
4.开始按钮
5.退出按钮

首先来实现文字显示部分

from tkinter import *

class UserDialog:
    _Ip = '127.0.0.1'
    _Port = 6000
    def __init__(self):
        pass
    @classmethod
    def GetUserInput(cls):
        Window = Tk()
        # 绘制三个label,grid确定行列
        Label(Window, text='请输入IP').grid(row = 0, column = 0)
        Label(Window, text='IP').grid(row = 1, column = 0)
        Label(Window, text='Port').grid(row = 2, column = 0)
    
        Window.mainloop()


接下来实现输入框

from tkinter import *

class UserDialog:
    _Ip = '127.0.0.1'
    _Port = 6000
    def __init__(self):
        pass
    @classmethod
    def GetUserInput(cls):
        Window = Tk()
        Label(Window, text='请输入IP').grid(row = 0, column = 0)
        Label(Window, text='IP').grid(row = 1, column = 0)
        Label(Window, text='Port').grid(row = 2, column = 0)
        # 导入两个输入框,bd设置输入框边框宽度
        e1 = Entry(Window, bd=5)
        e2 = Entry(Window, bd=5)
        # 设置输入框的位置
        e1.grid(row=1, column=1)
        e2.grid(row=2, column=1)

        Window.mainloop()

下面实现按钮

from tkinter import *

class UserDialog:
    _Ip = '127.0.0.1'
    _Port = 6000
    def __init__(self):
        pass
    @classmethod
    def GetUserInput(cls):
        def getUserIpAndPort():
            print("成功")
        def exit_program():
            exit()
        Window = Tk()

        Label(Window, text='请输入IP').grid(row = 0, column = 0)
        Label(Window, text='IP').grid(row = 1, column = 0)
        Label(Window, text='Port').grid(row = 2, column = 0)

        e1 = Entry(Window, bd=5)
        e2 = Entry(Window, bd=5)
        e1.grid(row=1, column=1)
        e2.grid(row=2, column=1)

        # 设置两个按钮,点击按钮执行命令 command= 命令函数
        button1 = Button(Window, text="开始", width=10, command=getUserIpAndPort)
        button2 = Button(Window, text="退出", width=10, command=getUserIpAndPort())
        # 设置按钮的位置行列及大小
        button1.grid(row=3, column=0, sticky=W, padx=10, pady=5)
        button2.grid(row=3, column=1, sticky=E, padx=10, pady=5)
        Window.mainloop()


接下来尝试获取输入的Ip和Port

        def getUserIpAndPort():
            cls._Ip = e1.get()
            cls._Port = e2.get()
            print(cls._Ip, cls._Port)

运行后在界面窗口输入如下信息



点击开始按钮我们就可以得到127.0.0.1和6000信息了,若点击退出,则退出当前窗口。

上一篇 下一篇

猜你喜欢

热点阅读