pyside2运行时未响应,但在pycharm手动关闭程序后能显示结果?

自己尝试编写了一个直播弹幕机,使用pyside2编写图形界面,调试的过程中发现输入房间号运行后窗口就未响应,在pycharm里结束程序后才能显示出弹幕信息,这个问题如何解决?

import asyncio

import requests

import time

import pymssql

import re

from threading import Thread

from lxml import html

etree = html.etree

from PySide2.QtWidgets import QApplication, QMessageBox

from PySide2.QtUiTools import QUiLoader

from PySide2.QtCore import QFile

#应用程序

class Danmu_app():

def __init__(self):

#加载UI文件

file = QFile("ui/bili_barrage.ui")

file.open(QFile.ReadOnly)

file.close()

self.ui = QUiLoader().load(file)

self.ui.start.clicked.connect(self.begin)

def begin(self):

info = self.ui.room_id.text() #info获取房间号

bilidanmu.get_roomid(info) #得到真实房间号

Thread(target=bilidanmu.get_danmu()).start() #开始获取弹幕

# bilidanmu.get_danmu()

#数据库

class sqlserver():

def __init__(self, host, user, pwd, db):

self.host = host #主机名

self.user = user #用户名

self.pwd = pwd #密码

self.db = db #数据库名

#连接数据库

def connect(self):

self.conn = pymssql.connect(host=self.host, user=self.user, password=self.pwd, database=self.db)

cur = self.conn.cursor()

if not cur:

print("无法连接数据库!")

else:

return cur

#添加数据

def insert_sql(self, sql, tpl):

cur = self.connect()

cur.execute(sql, tpl)

self.conn.commit()

self.conn.close()

#查询库中已经有的主播名

def quary_anchor_name(self):

sql = "select table_name from information_schema.tables where TABLE_CATALOG='Barrage'"

cur = self.connect()

cur.execute(sql)

data = cur.fetchall()

self.conn.commit()

self.conn.close()

return data

#执行查询命令

def execquary(self):

pass

#新建数据表

def creat_table(self, table_name):

sql = """CREATE TABLE [dbo].[{}](

[text] [varchar](100) NOT NULL,

[uid] [varchar](15) NOT NULL,

[nickname] [varchar](20) NOT NULL,

[timeline] [varchar](20) NOT NULL,

[medal_level] [varchar](5) NULL,

[medal_name] [varchar](10) NULL,

[medal_anchor] [varchar](20) NULL

)""".format(table_name)

cur = self.connect()

cur.execute(sql)

self.conn.commit()

self.conn.close()

#获取弹幕

class danmu():

def __init__(self):

# 请求弹幕url

self.url = 'https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory'

self.headers = {

'Host': 'api.live.bilibili.com',

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36',

}

self.log_file_write = open('danmu.log', mode='a', encoding='utf-8') #写入日志

log_file_read = open('danmu.log', mode='r', encoding='utf-8')

self.log = log_file_read.readlines()

# 获取真实房间号

def get_roomid(self, info):

#输入短房间号

short_id = info

roomurl = 'https://api.live.bilibili.com/room/v1/Room/room_init?id={}'.format(short_id)

html = requests.post(url=roomurl, headers=self.headers).json()

roomid = html['data']['room_id']

self.anchor_name = self.get_anchor_name(short_id)

self.creat_or_not(self.anchor_name)

self.room_id = roomid

#获取主播名字

def get_anchor_name(self,short_id):

url = "https://live.bilibili.com/{}".format(short_id)

headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36',

}

page_text = requests.get(url=url, headers=headers).text

res = etree.HTML(page_text)

r = res.xpath('//*[@id="link-app-title"]/text()')

result = re.search(r'- (.*) -', str(r[0])).span()

return str(r[0])[result[0] + 2: result[1] - 2]

# 判断是否已经接收过该主播的弹幕,如果没有则新建一个数据表

def creat_or_not(self, anchor_name):

anchor_names = dmsql.quary_anchor_name()

for t in anchor_names:

if t[0] == anchor_name:

return

dmsql.creat_table(anchor_name)

#获取弹幕信息

def get_danmu(self):

self.data = {

'roomid': self.room_id,

}

async def dm():

while True:

time.sleep(0.5)

html = requests.post(url = self.url, headers=self.headers, data=self.data).json()

#提取json中包含的弹幕信息

for content in html['data']['room']: #遍历所有弹幕

text = content['text'] #弹幕文本

uid = content['uid'] #发送者uid

nickname = content['nickname'] #发送者昵称

timeline = content['timeline'] #发送时间

if content['medal'] != []:

medal_level = content['medal'][0] #发送者粉丝牌等级

medal_name = content['medal'][1] #发送者粉丝牌名字

medal_anchor = content['medal'][2] #发送者粉丝牌所属主播

else:

medal_level = '无'

medal_name = '无'

medal_anchor = '无'

msg = timeline + ' ' + nickname + ': ' + text #要显示的发言

log_msg = msg + ' ' + 'uid:' + str(uid) + ' ' + str(medal_level) + ' ' + medal_name + ' ' + medal_anchor + '\n'

if log_msg not in self.log:

# 打印消息

# print(msg)

danmu_app.ui.barrage.appendPlainText(msg)

# 保存日志

self.log_file_write.write(log_msg)

# 添加到日志列表

self.log.append(log_msg)

#保存到数据库

#将存储数据保存为元组

tpl = (text, uid, nickname, timeline, medal_level, medal_name, medal_anchor)

sql = "insert into {} values (%s,%s,%s,%s,%s,%s,%s)".format(self.anchor_name) # 构造sql语句

dmsql.insert_sql(sql, tpl)

# dmsql.insert_sql(text, str(uid), nickname, timeline, str(medal_level), medal_name, medal_anchor)

# 清空变量缓存

nickname = ''

text = ''

timeline = ''

msg = ''

loop = asyncio.get_event_loop()

task = loop.create_task(dm())

loop.run_until_complete(task)

loop.run_forever()

#连接数据库

dmsql = sqlserver('XXX','sa','XXX','XXX')

bilidanmu = danmu()

app = QApplication([])

danmu_app = Danmu_app()

danmu_app.ui.show()

app.exec_()

执行时未响应:
pyside2运行时未响应,但在pycharm手动关闭程序后能显示结果?
结束运行后显示结果:
pyside2运行时未响应,但在pycharm手动关闭程序后能显示结果?

以上是 pyside2运行时未响应,但在pycharm手动关闭程序后能显示结果? 的全部内容, 来源链接: utcz.com/p/938402.html

回到顶部