python中面向对象的ui_python面向对象设计

这个博客展示了如何使用Python的PyQt5库创建一个简单的GUI应用,包括打开、保存文件的功能。同时,还介绍了如何搭建一个UDP服务器,接收并显示客户端发送的数据。
摘要由CSDN通过智能技术生成

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'

#

# Created by: PyQt5 UI code generator 5.13.0

#

# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

import sys

class Ui_Form(object):

def setupUi(self, Form):

Form.setObjectName("Form")

Form.resize(411, 300)

self.textEdit = QtWidgets.QTextEdit(Form)

self.textEdit.setGeometry(QtCore.QRect(0, 30, 411, 271))

self.textEdit.setObjectName("textEdit")

self.Form2 = QtWidgets.QPushButton(Form)

self.Form2.setGeometry(QtCore.QRect(80, 0, 75, 23))

self.Form2.setObjectName("Form2")

self.Form1 = QtWidgets.QPushButton(Form)

self.Form1.setGeometry(QtCore.QRect(0, 0, 75, 23))

self.Form1.setObjectName("Form1")

self.Form1.clicked.connect(self.openFile)

self.Form2.clicked.connect(self.saveFile)

self.retranslateUi(Form)

QtCore.QMetaObject.connectSlotsByName(Form)

def openFile(self):

fr=open("D:\\Project\\123.txt","r")

s=fr.read()

self.textEdit.setText(s)

fr.close()

def saveFile(self):

fr=open("D:\\Project\\123.txt","w")

s=fr.write(self.textEdit.toPlainText())

fr.close()

def retranslateUi(self, Form):

_translate = QtCore.QCoreApplication.translate

Form.setWindowTitle(_translate("Form", "我的记事本"))

self.Form2.setText(_translate("Form", "保存文件"))

self.Form1.setText(_translate("Form", "打开文件"))

if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)

form = QtWidgets.QWidget()

windows = Ui_Form()

windows.setupUi(form)

form.show()

sys.exit(app.exec_())

服务器采用:'127.0.0.1',9911

客户端:ip port随意

客户端发送数据,服务器端接受数据,并显示。

我的答案:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'server_udp.ui'

#

# Created by: PyQt5 UI code generator 5.13.0

#

# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

import sys

import socket

import _thread

class Ui_Form(object):

def setupUi(self, Form):

Form.setObjectName("Form")

Form.resize(405, 300)

self.client_addr=('127.0.0.1', 9911)

self.server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

self.textBrowser = QtWidgets.QTextBrowser(Form)

self.textBrowser.setGeometry(QtCore.QRect(0, 0, 401, 221))

self.textBrowser.setObjectName("textBrowser")

self.horizontalLayoutWidget = QtWidgets.QWidget(Form)

self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 220, 401, 80))

self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")

self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)

self.horizontalLayout.setContentsMargins(0, 0, 0, 0)

self.horizontalLayout.setObjectName("horizontalLayout")

self.textEdit = QtWidgets.QTextEdit(self.horizontalLayoutWidget)

self.textEdit.setObjectName("textEdit")

self.horizontalLayout.addWidget(self.textEdit)

self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget)

self.pushButton.setObjectName("pushButton")

_thread.start_new_thread(self.socket_listen,())

self.pushButton.clicked.connect(lambda:self.send_msg())

self.retranslateUi(Form)

QtCore.QMetaObject.connectSlotsByName(Form)

# def get_msg(self):

#     while True:

#         data,server_addr=self.client.recvfrom(1024)

#         self.textBrowser.append(str(self.client_addr) + str(":") + str(data, encoding="stf-8"))

def socket_listen(self):

BUFSIZE=1024

ip_port = ('192.168.27.128', 9911)

self.server.bind(ip_port)

while True:

data, self.client_addr = self.server.recvfrom(BUFSIZE)

self.textBrowser.append(str(self.client_addr)+str(":")+str(data,encoding="utf-8"))

server.close()

def send_msg(self):

data=self.textEdit.toPlainText()

self.server.sendto(bytes(data,encoding='utf8'),self.client_addr)

def retranslateUi(self, Form):

_translate = QtCore.QCoreApplication.translate

Form.setWindowTitle(_translate("Form", "服务器端"))

self.pushButton.setText(_translate("Form", "发送"))

if __name__== "__main__":

app=QtWidgets.QApplication(sys.argv)

widget=QtWidgets.QWidget()

ui=Ui_Form()

ui.setupUi(widget)

widget.show()

sys.exit(app.exec_())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值