[Python] 纯文本查看 复制代码
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import tkinter
from tkinter import LabelFrame
from tkinter import Tk
from tkinter import ttk
from tkinter import Text
from tkinter import LabelFrame
from tkinter import Label
from tkinter import StringVar
from tkinter import Entry
from tkinter import END
from tkinter import Button
from tkinter import Frame
from tkinter import RIGHT
from tkinter import NSEW
from tkinter import NS
from tkinter import NW
from tkinter import N
from tkinter import Y
from tkinter import DISABLED
from tkinter import NORMAL
from re import findall
from json import loads
from threading import Thread
from urllib.parse import quote
from webbrowser import open
import os
import ssl
import socket
def thread_it(func, *args):
'''
将函数打包进线程
'''
# 创建
t = Thread(target=func, args=args)
# 守护
t.setDaemon(True)
# 启动
t.start()
def handlerAdaptor(fun, **kwds):
'''事件处理函数的适配器,相当于中介,那个event是从那里来的呢,我也纳闷,这也许就是python的伟大之处吧'''
return lambda event, fun=fun, kwds=kwds: fun(event, **kwds)
class uiObject:
def __init__(self):
self.jsonData = ""
self.jsonData_keyword = ""
def center_window(self, root, w, h):
"""
窗口居于屏幕中央
:param root: root
:param w: 窗口宽度
:param h: 窗口高度
:return:
"""
# 获取屏幕 宽、高
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
# 计算 x, y 位置
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
def tcp_conn(self):
ip = self.T_serverIP.get()
port = self.T_serverPort.get()
msg = self.T_Msg.get("1.0", "end")
addr = (ip, int(port))
clientsock = socket.socket() ## 创建一个socket
clientsock.connect(addr) # 建立连接
#say = input("输入你想传送的消息:")
clientsock.send(bytes(msg, encoding='gbk')) # 发送消息
recvdata = clientsock.recv(1024) # 接收消息 recvdata 是bytes形式的
self.Rt_Msg.insert(0.0, str(recvdata, encoding='gbk'))
print(str(recvdata, encoding='gbk')) # 我们看不懂bytes,所以转化为 str
clientsock.close()
def ui_process(self):
"""
Ui主程序
:param
:return:
"""
# 初始化Tk()
root = tkinter.Tk()
self.root = root
# 设置窗口位置
root.title("豆瓣电影小助手(可筛选、下载自定义电影)----吾爱破解论坛 [url=http://www.52pojie.cn]www.52pojie.cn[/url]")
self.center_window(root, 1000, 565)
root.resizable(0, 0) # 框体大小可调性,分别表示x,y方向的可变性
# 容器控件
labelframe = LabelFrame(root, width=360, height=450, text="前置服务端")
labelframe.place(x=5, y=5)
self.labelframe = labelframe
# 服务端
S_serverIP = Label(labelframe, text='服务端IP')
S_serverIP.place(x=0, y=10)
self.S_serverIP = S_serverIP
# 文本框
T_serverIP = Entry(labelframe, width=40)
T_serverIP.delete(0, END)
T_serverIP.insert(0, '127.0.0.1')
T_serverIP.place(x=60, y=10)
self.T_serverIP = T_serverIP
# 服务端端口
S_serverPort = Label(labelframe, text='服务端口')
S_serverPort.place(x=0, y=50)
self.S_serverPort = S_serverPort
# 文本框
T_serverPort = Entry(labelframe, width=40)
T_serverPort.delete(0, END)
T_serverPort.insert(0, '6000')
T_serverPort.place(x=60, y=50)
self.T_serverPort = T_serverPort
S_result = Label(labelframe, text="未连接", bg="green", font=("Arial", 10), width=5, height=1)
S_result.place(x=70, y=80)
#报文内容
S_Msg = Label(labelframe, text='报文内容')
S_Msg.place(x=0, y=80)
self.S_Msg = S_Msg
#报文内容
T_Msg=Text(labelframe, width=50, height=14,)
T_Msg.pack()
T_Msg.place(x=0, y=100)
self.T_Msg = T_Msg
B_S = Button(labelframe, text="发送", )
B_S.place(x=310, y=300)
self.B_S = B_S
# 容器控件 right 2222222222222222222222222222222222222222
labelframe1 = LabelFrame(root, width=360, height=450, text="接收报文展示")
labelframe1.place(x=365, y=5)
self.labelframe1 = labelframe1
#接收报文展示内容
Rt_Msg=Text(labelframe1, width=50, height=14,)
Rt_Msg.pack()
Rt_Msg.place(x=0, y=0)
self.Rt_Msg = Rt_Msg
#报文内容
TT_Msg=Text(labelframe1, width=50, height=14,)
TT_Msg.pack()
TT_Msg.place(x=0, y=240)
self.TT_Msg = TT_Msg
#绑定事件
B_S.configure(command=lambda: thread_it(self.tcp_conn()))
#B_S.configure(command=self.tcp_conn())
# 进入消息循环
root.mainloop()
if __name__ == '__main__':
ui = uiObject()
ui.ui_process()