可以帮忙改一下代码加上服务器连接吗

import sys

import os

import sqlite3

from PyQt5.QtWidgets import QDialog

from PyQt5.QtCore import Qt, pyqtSignal, QThread, QObject, QIODevice

from PyQt5.QtGui import QPixmap, QImage, QIcon, QStandardItemModel, QStandardItem

from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout, QVBoxLayout, QHBoxLayout, QLabel, \

    QLineEdit, QPushButton, QTextEdit, QListWidget, QListWidgetItem, QFileDialog, QMessageBox, QTableView, \

    QHeaderView, QAbstractItemView, QComboBox


 

class LoginWindow(QWidget):

    login_signal = pyqtSignal(str)

    def __init__(self):

        super().__init__()

        self.setWindowTitle("登录")

        self.setFixedSize(400, 250)

        self.username_label = QLabel("用户名:")

        self.username_edit = QLineEdit()

        self.username_edit.setPlaceholderText("请输入用户名")

        self.password_label = QLabel("密码:")

        self.password_edit = QLineEdit()

        self.password_edit.setPlaceholderText("请输入密码")

        self.password_edit.setEchoMode(QLineEdit.Password)

        self.login_button = QPushButton("登录")

        self.login_button.clicked.connect(self.login)

        self.register_button = QPushButton("注册")

        self.register_button.clicked.connect(self.register)

        layout = QGridLayout()

        layout.addWidget(self.username_label, 0, 0, 1, 1)

        layout.addWidget(self.username_edit, 0, 1, 1, 3)

        layout.addWidget(self.password_label, 1, 0, 1, 1)

        layout.addWidget(self.password_edit, 1, 1, 1, 3)

        layout.addWidget(self.login_button, 2, 1, 1, 1)

        layout.addWidget(self.register_button, 2, 2, 1, 1)

        self.setLayout(layout)

    def login(self):

        username = self.username_edit.text().strip()

        password = self.password_edit.text().strip()

        if not username or not password:

            QMessageBox.warning(self, "警告", "用户名和密码不能为空!")

            return

        conn = sqlite3.connect("data.db")

        c = conn.cursor()

        c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))

        result = c.fetchone()

        conn.close()

        if result:

            QMessageBox.information(self, "提示", "登录成功!")

            self.login_signal.emit(username)

            self.close()

        else:

            QMessageBox.warning(self, "警告", "用户名或密码错误!")

    def register(self):

        register_window = RegisterWindow()

        register_window.exec_()

class RegisterWindow(QDialog):

    def __init__(self):

        super().__init__()

        self.setWindowTitle("注册")

        self.setFixedSize(400, 250)

        self.username_label = QLabel("用户名:")

        self.username_edit = QLineEdit()

        self.username_edit.setPlaceholderText("请输入用户名")

        self.password_label = QLabel("密码:")

        self.password_edit = QLineEdit()

        self.password_edit.setPlaceholderText("请输入密码")

        self.password_edit.setEchoMode(QLineEdit.Password)

        self.confirm_label = QLabel("确认密码:")

        self.confirm_edit = QLineEdit()

        self.confirm_edit.setPlaceholderText("请再次输入密码")

        self.confirm_edit.setEchoMode(QLineEdit.Password)

        self.register_button = QPushButton("注册")

        self.register_button.clicked.connect(self.register)

        layout = QGridLayout()

        layout.addWidget(self.username_label, 0, 0, 1, 1)

        layout.addWidget(self.username_edit, 0, 1, 1, 3)

        layout.addWidget(self.password_label, 1, 0, 1, 1)

        layout.addWidget(self.password_edit, 1, 1, 1, 3)

        layout.addWidget(self.confirm_label, 2, 0, 1, 1)

        layout.addWidget(self.confirm_edit, 2, 1, 1, 3)

        layout.addWidget(self.register_button, 3, 2, 1, 1)

        self.setLayout(layout)

    def register(self):

        username = self.username_edit.text().strip()

        password = self.password_edit.text().strip()

        confirm = self.confirm_edit.text().strip()

        if not username or not password or not confirm:

            QMessageBox.warning(self, "警告", "用户名和密码不能为空!")

            return

        if password != confirm:

            QMessageBox.warning(self, "警告", "两次输入的密码不一致!")

            return

        conn = sqlite3.connect("data.db")

        c = conn.cursor()

        c.execute("SELECT * FROM users WHERE username=?", (username,))

        result = c.fetchone()

        if result:

            QMessageBox.warning(self, "警告", "用户名已存在!")

        else:

            c.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))

            conn.commit()

            QMessageBox.information(self, "提示", "注册成功!")

            self.close()

        conn.close()


 

class PostThread(QThread):

    post_signal = pyqtSignal(str)

    def __init__(self, username, title, content, images):

        super().__init__()

        self.username = username

        self.title = title

        self.content = content

        self.images = images

    def run(self):

        conn = sqlite3.connect("data.db")

        c = conn.cursor()

        c.execute("INSERT INTO posts (username, title, content) VALUES (?, ?, ?)", (self.username, self.title, self.content))

        post_id = c.lastrowid

        for image in self.images:

            with open(image, "rb") as f:

                data = f.read()

                c.execute("INSERT INTO images (post_id, data) VALUES (?, ?)", (post_id, data))

        conn.commit()

        conn.close()

        self.post_signal.emit("success")


 

class PostWindow(QWidget):

    def __init__(self, username):

        super().__init__()

        self.setWindowTitle("发帖")

        self.setFixedSize(600, 400)

        self.username = username

        self.title_label = QLabel("标题:")

        self.title_edit = QLineEdit()

        self.title_edit.setPlaceholderText("请输入标题")

        self.content_label = QLabel("内容:")

        self.content_edit = QTextEdit()

        self.image_label = QLabel("图片:")

        self.image_list = QListWidget()

        self.image_list.setFixedHeight(80)

        self.add_image_button = QPushButton("添加图片")

        self.add_image_button.clicked.connect(self.add_image)

        self.remove_image_button = QPushButton("删除图片")

        self.remove_image_button.clicked.connect(self.remove_image)

        self.post_button = QPushButton("发布")

        self.post_button.clicked.connect(self.post)

        layout = QVBoxLayout()

        top_layout = QHBoxLayout()

        top_layout.addWidget(self.title_label)

        top_layout.addWidget(self.title_edit)

        layout.addLayout(top_layout)

        layout.addWidget(self.content_label)

        layout.addWidget(self.content_edit)

        image_layout = QHBoxLayout()

        image_layout.addWidget(self.image_label)

        image_layout.addWidget(self.image_list)

        layout.addLayout(image_layout)

        button_layout = QHBoxLayout()

        button_layout.addWidget(self.add_image_button)

        button_layout.addWidget(self.remove_image_button)

        button_layout.addStretch(1)

        button_layout.addWidget(self.post_button)

        layout.addLayout(button_layout)

        self.setLayout(layout)

    def add_image(self):

        file_dialog = QFileDialog()

        file_dialog.setFileMode(QFileDialog.ExistingFiles)

        file_dialog.setNameFilter("Images (*.png *.xpm *.jpg *.bmp)")

        if file_dialog.exec_():

            files = file_dialog.selectedFiles()

            for file in files:

                item = QListWidgetItem(QIcon(file), os.path.basename(file))

                item.setData(Qt.UserRole, file)

                self.image_list.addItem(item)

    def remove_image(self):

        for item in self.image_list.selectedItems():

            self.image_list.takeItem(self.image_list.row(item))

    def post(self):

        title = self.title_edit.text().strip()

        content = self.content_edit.toPlainText().strip()

        images = [self.image_list.item(i).data(Qt.UserRole) for i in range(self.image_list.count())]

        if not title or not content:

            QMessageBox.warning(self, "警告", "标题和内容不能为空!")

            return

        if not images:

            QMessageBox.warning(self, "警告", "请至少添加一张图片!")

            return

        thread = PostThread(self.username, title, content, images)

        thread.post_signal.connect(self.post_result)

        thread.start()

    def post_result(self, result):

        if result == "success":

            QMessageBox.information(self, "提示", "发布成功!")

            self.close()


 

class CommentThread(QThread):

    comment_signal = pyqtSignal(str)

    def __init__(self, username, post_id, content, image):

        super().__init__()

        self.username = username

        self.post_id = post_id

        self.content = content

        self.image = image

    def run(self):

        conn = sqlite3.connect("data.db")

        c = conn.cursor()

        c.execute("INSERT INTO comments (username, post_id, content) VALUES (?, ?, ?)", (self.username, self.post_id, self.content))

        comment_id = c.lastrowid

        if self.image:

            with open(self.image, "rb") as f:

                data = f.read()

                c.execute("INSERT INTO comment_images (comment_id, data) VALUES (?, ?)", (comment_id, data))

        conn.commit()

        conn.close()

        self.comment_signal.emit("success")


 

class CommentWindow(QWidget):

    def __init__(self, username, post_id):

        super().__init__()

        self.setWindowTitle("评论")

        self.setFixedSize(400, 300)

        self.username = username

        self.post_id = post_id

        self.content_label = QLabel("内容:")

        self.content_edit = QTextEdit()

        self.image_label = QLabel("图片:")

        self.image_edit = QLineEdit()

        self.image_edit.setReadOnly(True)

        self.choose_image_button = QPushButton("选择图片")

        self.choose_image_button.clicked.connect(self.choose_image)

        self.comment_button = QPushButton("评论")

        self.comment_button.clicked.connect(self.comment)

        layout = QVBoxLayout()

        layout.addWidget(self.content_label)

        layout.addWidget(self.content_edit)

        image_layout = QHBoxLayout()

        image_layout.addWidget(self.image_label)

        image_layout.addWidget(self.image_edit)

        image_layout.addWidget(self.choose_image_button)

        layout.addLayout(image_layout)

        button_layout = QHBoxLayout()

        button_layout.addStretch(1)

        button_layout.addWidget(self.comment_button)

        layout.addLayout(button_layout)

        self.setLayout(layout)

    def choose_image(self):

        file_dialog = QFileDialog()

        file_dialog.setFileMode(QFileDialog.ExistingFile)

        file_dialog.setNameFilter("Images (*.png *.xpm *.jpg *.bmp)")

        if file_dialog.exec_():

            file = file_dialog.selectedFiles()[0]

            self.image_edit.setText(file)

    def comment(self):

        content = self.content_edit.toPlainText().strip()

        image = self.image_edit.text().strip()

        if not content:

            QMessageBox.warning(self, "警告", "内容不能为空!")

            return

        thread = CommentThread(self.username, self.post_id, content, image)

        thread.comment_signal.connect(self.comment_result)

        thread.start()

    def comment_result(self, result):

        if result == "success":

            QMessageBox.information(self, "提示", "评论成功!")

            self.close()


 

class LikeThread(QThread):

    like_signal = pyqtSignal(str)

    def __init__(self, username, post_id):

        super().__init__()

        self.username = username

        self.post_id = post_id

    def run(self):

        conn = sqlite3.connect("data.db")

        c = conn.cursor()

        c.execute("INSERT INTO likes (username, post_id) VALUES (?, ?)", (self.username, self.post_id))

        conn.commit()

        conn.close()

        self.like_signal.emit("success")


 

class UnlikeThread(QThread):

    unlike_signal = pyqtSignal(str)

    def __init__(self, username, post_id):

        super().__init__()

        self.username = username

        self.post_id = post_id

    def run(self):

        conn = sqlite3.connect("data.db")

        c = conn.cursor()

        c.execute("DELETE FROM likes WHERE username=? AND post_id=?", (self.username, self.post_id))

        conn.commit()

        conn.close()

        self.unlike_signal.emit("success")


 

class MainWindow(QMainWindow):

    def __init__(self):

        super().__init__()

        self.setWindowTitle("图像交流")

        self.setFixedSize(800, 600)

        self.username = None

        self.login_action = self.menuBar().addAction("登录")

        self.login_action.triggered.connect(self.show_login)

        self.logout_action = self.menuBar().addAction("注销")

        self.logout_action.triggered.connect(self.logout)

        self.logout_action.setVisible(False)

        self.post_action = self.menuBar().addAction("发帖")

        self.post_action.triggered.connect(self.show_post)

        self.post_action.setVisible(False)

        self.comment_action = self.menuBar().addAction("评论")

        self.comment_action.triggered.connect(self.show_comment)

        self.comment_action.setVisible(False)

        self.like_action = self.menuBar().addAction("点赞")

        self.like

帮忙加上数据库和密码的变量以及操作,都是root ip写1.1.1.1即可不方便给 端口 8888即可

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上海天航芸科技(集团)有限公司

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值