python面向对象设计

界面如下所示

python面向对象设计

 

 

我的答案:

# -*- 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_Dialog(object):

    def setupUi(self, Dialog):

        Dialog.setObjectName("Dialog")

        Dialog.resize(400, 300)

        self.num=0

        self.pushButton = QtWidgets.QPushButton(Dialog)

        self.pushButton.setGeometry(QtCore.QRect(270, 190, 75, 23))

        self.pushButton.setObjectName("pushButton")

        self.pushButton.clicked.connect(self.leijia)

        self.lineEdit = QtWidgets.QLineEdit(Dialog)

        self.lineEdit.setGeometry(QtCore.QRect(90, 190, 113, 20))

        self.lineEdit.setObjectName("lineEdit")

        self.label = QtWidgets.QLabel(Dialog)

        self.label.setGeometry(QtCore.QRect(90, 150, 54, 12))

        self.label.setObjectName("label")

 

        self.retranslateUi(Dialog)

        QtCore.QMetaObject.connectSlotsByName(Dialog)

 

    def leijia(self):

        self.num=self.num+1

        self.lineEdit.setText(str(self.num))

    def retranslateUi(self, Dialog):

        _translate = QtCore.QCoreApplication.translate

        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))

        self.pushButton.setText(_translate("Dialog", "累加"))

        self.label.setText(_translate("Dialog", "显示数字:"))

if __name__== "__main__":

    app=QtWidgets.QApplication(sys.argv)

    widget=QtWidgets.QWidget()

    ui=Ui_Dialog()

    ui.setupUi(widget)

    widget.show()

    sys.exit(app.exec_())

           

绘制如下图形,当滚动时,并将当前dial的值显示在lineEdit中

python面向对象设计

 

我的答案:

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

 

# Form implementation generated from reading ui file 'untitled3.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(400, 300)

        self.lineEdit = QtWidgets.QLineEdit(Form)

        self.lineEdit.setGeometry(QtCore.QRect(120, 210, 113, 20))

        self.lineEdit.setObjectName("lineEdit")

        self.dial = QtWidgets.QDial(Form)

        self.dial.setGeometry(QtCore.QRect(120, 70, 111, 111))

        self.dial.setObjectName("dial")

        self.dial.sliderMoved.connect(self.dial_num)

        self.retranslateUi(Form)

        QtCore.QMetaObject.connectSlotsByName(Form)

    def dial_num(self):

        self.lineEdit.setText(str(self.dial.value()))

    def retranslateUi(self, Form):

        _translate = QtCore.QCoreApplication.translate

        Form.setWindowTitle(_translate("Form", "Form"))

if __name__== "__main__":

    app=QtWidgets.QApplication(sys.argv)

    widget=QtWidgets.QWidget()

    ui=Ui_Form()

    ui.setupUi(widget)

    widget.show()

    sys.exit(app.exec_())

 

 

 

 

 

 

 

打开D:\Project\123.txt文件,并在修改后保存到123.txt

python面向对象设计

 

我的答案:

# -*- 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_())

 

 

 

 

 

 

 

 

 

 

上一篇:Qt-QLabel中图像的缩放、旋转、镜像


下一篇:第二章:ObpLookupObjectName 函数