python 里用tkinter写入Text数据时报错。求酬解
button 绑定事件为
def wuyu():
text.insert(1.0,mumu())
提示出错为:
self.tk.call((self._w, 'insert', index, chars) + args)
_tkinter.TclError: wrong # args: should be ".!text insert index chars ?tagList chars tagList ...?"
网上找不到资料!
把相关代码贴全了,看看
你把你得代码发给我看看
下面是我的代码
```python
import tkinter as tk
from tkinter import messagebox
from protoc.cs.prtoc_pb2 import cs as cs
from protoc.roomid.prtoc_pb2 import roomid as roomid
from protoc.ping.prtoc_pb2 import ping
import websocket
import base64,time,os
import _thread as thread
#调用token,streamid,wss链接引入模块
import lxml.etree,json
import requests
import re
import os
os.environ['NO_PROXY'] = 'stackoverflow.com'
#调用token,streamid,wss链接引入模块 结束
#这个模块是搜索用的
import urllib.parse
#调用时间戳引入模块
import random
import time
win = tk.Tk()
win.title("采集测试")
# win.iconbitmap('C:/Users/Administrator/Desktop/C语言中文网logo.ico')
win.geometry('800x1000+100+100')
win.resizable(0,0)
input = tk.Entry(win, width=50)
input.grid(row=0, column=1, padx=10, pady=5)
input.insert(0,'https://live.kuaishou.com/u/3xahgwght945saa')
# uids = []
uids = []
dd = ''
charset = "bjectSymhasOwnProp-0123456789ABCDEFGHIJKLMNQRTUVWXYZ_dfgiklquvxz"
a1 = random.sample(charset, 15)
a2 = dd.join(a1)
a3 = int(time.time()*1000)
a4 = a2 + '_' + str(a3)
#开始引入token,livestream,wss链接
url_api = 'https://live.kuaishou.com/live_graphql'
url_api_search = 'https://www.kuaishou.com/graphql'
Cookie = 'clientid=3; did=web_90ba3b4fbd4de0e234ac210c26aa027c; client_key=65890b29; kpn=GAME_ZONE; ksliveShowClipTip=true; didv=1667233764000; userId=3063961986; kuaishou.live.bfb1s=477cb0011daca84b36b3a4676857e5a1; userId=3063961986; kuaishou.live.web_st=ChRrdWFpc2hvdS5saXZlLndlYi5zdBKgAVo_EPCZ9v2tlVVBrouF1a6460lZLhaWeME4SNFC5_rKxUh5nKWutzMcYBD7VumTYzl9XnOYPnCULk74SRdGQBu_gR2duFRN1jTri-poEhEacXE7UWtObIUGT74hFKsRfW3ADkGkfpocMXT4kCzKAJIdB5UWi-Rk2k1Bwhnaz84yJpWuGIlM2mhZleKh-mGaPF6SYRdPN_NND-ivxjIvUPQaEoMRdS355EPfvO-WEFcOv_Ls2yIgNPcBVACH5zIOh4jYUmBpdap6RnOIlpyxDt0DEnisze4oBTAB; kuaishou.live.web_ph=dee65c8e06f34d65be348cbaad467c03744d'
query = "query WebSocketInfoQuery($liveStreamId: String) {\n webSocketInfo(liveStreamId: $liveStreamId) {\n token\n webSocketUrls\n __typename\n }\n}\n"
def wuyu():
text.insert(1.0,mumu())
def mumu():
global res_list,res_url
try:
headers = {
'Referer': input.get(),
'content-type': 'application/json',
'Origin': 'https://www.kuaishou.com',
'Cookie': Cookie,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}
resp = requests.get(url=input.get(), headers=headers).text
print(resp)
js = lxml.etree.HTML(resp).find('.//body/script').text
jsonCode = js.partition('liveStreamId":"')[2].strip()
fin_data = jsonCode.split('","caption"')[0]
data = {
'operationName': "WebSocketInfoQuery",
'query':query,
'variables': {'liveStreamId': fin_data}
# livestreamid
}
resp = requests.post(url=url_api, headers=headers, json=data)
res_text = resp.json()
res_list = res_text['data']['webSocketInfo']['token'] # token
res_url = res_text['data']['webSocketInfo']['webSocketUrls'][0] # wss链接
# uids.append(resp)
except:
pass
def on_message(ws, message):
try:
#保存返回的msg
# t = time.time()*1000
# t = str(int(t))
# msg_path = "msg/"+t+".bin"
# with open(msg_path, "wb") as file:
# file.write(message)
# path = os.path.abspath('protoc/protoc.exe')
# #调用cmd解析二进制
# cmd = path + " --decode_raw < " + msg_path
# os.system(cmd)
jiexi(message)
except:
pass
def on_error(ws, error):
pass
def on_open(ws):
def run():
roomid_msg_bin = init_base64()
ws.send(roomid_msg_bin, opcode=0x2)
def ping():
while True:
#print("开始自定义ping")
ping_msg_bin = init_ping()
ws.send(ping_msg_bin, opcode=0x2)
time.sleep(60)
thread.start_new_thread(run, ())
thread.start_new_thread(ping, ())
def on_close(ws,msg,type):
print("### closed ###")
def btn_click():
text.insert(1.0,jiexi('result'))
def jiexi(result):
try:
danmu_msg.ParseFromString(result)
if danmu_msg.messages[0].messages is not None:
for msg in danmu_msg.messages[0].messages:
if msg.messages[0].aaa1 not in uids:
uids.append(msg.messages[0].aaa1)
fin_danmu = msg.messages[0].aaa2 + "----说----" + msg.aa53
return fin_danmu
except:
pass
def init_file():
roomid_msg = roomid()
with open('msg/websocket_000001_client.bin', 'rb') as f:
roomid_msg_bin = f.read()
roomid_msg.ParseFromString(roomid_msg_bin)
roomid_msg.room = 200
roomid_msg.messages[
0].rooma1 = f"{res_list}"
roomid_msg.messages[0].rooma2 = f"{fin_data}"
roomid_msg.messages[0].rooma7 = a4
roomid_msg_bin = roomid_msg.SerializeToString()
return roomid_msg_bin
def init_base64():
roomid_msg = roomid()
roomid_msg_base64 = "CMgBGtwBCqwBdEZQWmpXcFA0dVlWc2dBZFFOcXBEbTBNdWJkOFBYbkdBZkNQQ1BOemhQcG1udjFrNW40TGUvV0syYkRIbVRLVVgvUW1CRE1hc1dlWjdpRUZZZmVGSGZ5YkhDdEZRbGUrUTlQSFQwOTBOajVlY2ExeHAwZitTVGswSXFoa3ZidE8wZE4rYVRlZE95SUIxOXlYVERQeWxpRDZMMWZSRzl4bmVLb0tadndxUk9vPRILZnBJcWNielNBMVk6HmVQelhZay1idFRJSEJ2bkNfMTY2OTUxOTYzMjc5Mg=="
roomid_msg_bin = base64.b64decode(roomid_msg_base64)
roomid_msg.ParseFromString(roomid_msg_bin)
#print(roomid_msg)
roomid_msg_bin = roomid_msg.SerializeToString()
return roomid_msg_bin
def init_ping():
#自定义ping
ping_msg = ping()
ping_msg_base64 = "CAEaBwjvy/DnyjA="
ping_msg_bin = base64.b64decode(ping_msg_base64)
ping_msg.ParseFromString(ping_msg_bin)
ping_msg.messages[0].ping31 = int(time.time()*1000)
roomid_msg_bin = ping_msg.SerializeToString()
return roomid_msg_bin
# if __name__ == "__main__":
# ping_time = int(time.time())
# danmu_msg = cs()
# #websocket.enableTrace(True)
# url = f'{res_url}'
# ws = websocket.WebSocketApp(url,on_message=on_message,on_error=on_error,on_close=on_close)
# ws.on_open = on_open
# ws.run_forever(ping_interval=60,ping_timeout=5)
# def daili():
#
# mumu('abc')
bt1 = tk.Button(win, text="打开", width=10, command=wuyu)
bt1.grid(row=3, column=0, sticky="w", padx=10, pady=5)
bt2 = tk.Button(win, text="退出", width=10, command=win.quit)
bt2.grid(row=3, column=1, sticky="e", padx=10, pady=5)
text = tk.Text(win,width=50,height=100)
text.grid()
win.mainloop()
```
mumu()这个函数没有返回值,不想插入任何值,返回一个空字符串
def mumu()
return ""