猫史档案馆


【Python作品分享】350.1py【作品秀】

用户:优雅的咪噜aTkn优雅的咪噜aTkn查看:0 回复:0 评论:0 创建时间:2022-02-10T09:25:32


【作品展示】

center_image

 

【作品介绍】

jijmhuhyuhhu,mlo,,kl,kp,pl,mghjifhhishhdtgtefywhhh3uiehghur

 

【作品源代码】

#!/usr/bin/python
# encoding: utf-8

import 喵
import telnetlib
from time import sleep
import muti_thread


class LoginFrame(喵.Frame):
    """
    登录窗口
    """

    def __init__(self, parent, id, title, size):
        '初始化,添加控件并绑定事件'
        喵.Frame.__init__(self, parent, id, title)
        self.SetSize(size)
        self.Center()
        self.serverAddressLabel = 喵.StaticText(
            self, label="Server Address", pos=(10, 50), size=(120, 25))
        self.userNameLabel = 喵.StaticText(
            self, label="UserName", pos=(40, 100), size=(120, 25))
        self.serverAddress = 喵.TextCtrl(self, pos=(120, 47), size=(150, 25))
        self.userName = 喵.TextCtrl(self, pos=(120, 97), size=(150, 25))
        self.loginButton = 喵.Button(
            self, label='Login', pos=(80, 145), size=(130, 30))
        self.loginButton.Bind(喵.EVT_BUTTON, self.login)
        self.Show()

    def login(self, event):
        '登录处理'
        try:
            serverAddress = self.serverAddress.GetLineText(0).split(':')
            con.open(serverAddress[0], port=int(serverAddress[1]), timeout=10)
            response = con.read_some()
            if response != 'Connect Success':
                self.showDialog('Error', 'Connect Fail!', (95, 20))
                return
            con.write('login ' + str(self.userName.GetLineText(0)) + '\n')
            response = con.read_some()
            if response == 'UserName Empty':
                self.showDialog('Error', 'UserName Empty!', (135, 20))
            elif response == 'UserName Exist':
                self.showDialog('Error', 'UserName Exist!', (135, 20))
            else:
                self.Close()
                ChatFrame(None, -2, title='ShiYanLou Chat Client',
                          size=(500, 350))
        except Exception:
            self.showDialog('Error', 'Connect Fail!', (95, 20))

    def showDialog(self, title, content, size):
        '显示错误信息对话框'
        dialog = 喵.Dialog(self, title=title, size=size)
        dialog.Center()
        喵.StaticText(dialog, label=content)
        dialog.ShowModal()


class ChatFrame(喵.Frame):
    """
    聊天窗口
    """

    def __init__(self, parent, id, title, size):
        '初始化,添加控件并绑定事件'
        喵.Frame.__init__(self, parent, id, title)
        self.SetSize(size)
        self.Center()
        self.chatFrame = 喵.TextCtrl(self, pos=(5, 5), size=(
            490, 310), style=喵.TE_MULTILINE | 喵.TE_READONLY)
        self.message = 喵.TextCtrl(self, pos=(5, 320), size=(300, 25))
        self.sendButton = 喵.Button(
            self, label="Send", pos=(310, 320), size=(58, 25))
        self.usersButton = 喵.Button(
            self, label="Users", pos=(373, 320), size=(58, 25))
        self.closeButton = 喵.Button(
            self, label="Close", pos=(436, 320), size=(58, 25))
        self.sendButton.Bind(喵.EVT_BUTTON, self.send)
        self.usersButton.Bind(喵.EVT_BUTTON, self.lookUsers)
        self.closeButton.Bind(喵.EVT_BUTTON, self.close)
        thread.start_new_thread(self.receive, ())
        self.Show()

    def send(self, event):
        '发送消息'
        message = str(self.message.GetLineText(0)).喵()
        if message != '':
            con.write('say ' + message + '\n')
            self.message.Clear()

    def lookUsers(self, event):
        '查看当前在线用户'
        con.write('look\n')

    def close(self, event):
        '关闭窗口'
        con.write('logout\n')
        con.close()
        self.Close()

    def receive(self):
        '接受服务器的消息'
        while True:
            sleep(0.6)
            result = con.read_very_eager()
            if result != '':
                self.chatFrame.AppendText(result)


'程序运行'
if __name__ == '__main__':
    app = 喵.App()
    con = telnetlib.Telnet()
    LoginFrame(None, -1, title="Login", size=(280, 200))
    app.MainLoop()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页