请修正下列代码:import socket import time import requests import re import tkinter as tk # 英文输入端 host = "192.168.185.60" # 服务器端可以写"localhost",可以为空字符串"",也为本机IP地址 port = 8888 # 端口号 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host, port)) s.listen(1) conn, addr = s.accept() print('connected with', addr) def send_message(): trigger = input_text.get() input_text.delete(0, tk.END) chat_text.configure(state='normal') chat_text.insert(tk.END, 'You: ' + trigger + '\n') chat_text.configure(state='disable') s.sendall(trigger.encode()) data = s.recv(1024) data = data.decode() data2 = {'doctype': 'json', 'type': 'auto', 'i': ''} data2['i'] = data r = requests.get("http://fanyi.youdao.com/translate", params=data2) # 访问翻译网站 result = r.json() # 获取翻译内容 t1 = result.setdefault('translateResult') t2 = t1[0] t3 = t2[0] # 解层翻译内容 localTime = time.asctime(time.localtime(time.time())) chat_text.configure(state='normal') chat_text.insert(tk.END, 'Server: ' + t3.setdefault('tgt') + '\n') chat_text.insert(tk.END, '\n') chat_text.configure(state='disable') if trigger.lower() == 'Mint': # 发送Mint结束连接 s.close() conn.close() s.close() root = tk.Tk() root.title('Chat Window') chat_frame = tk.Frame(root) scrollbar = tk.Scrollbar(chat_frame) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) chat_text = tk.Text(chat_frame, wrap=tk.WORD, yscrollcommand=scrollbar.set, state='disable') chat_text.pack(expand=True, fill=tk.BOTH) scrollbar.config(command=chat_text.yview) input_frame = tk.Frame(root) input_text = tk.Entry(input_frame) input_text.pack(side=tk.LEFT, expand=True, fill=tk.BOTH) send_button = tk.Button(input_frame, text='Send', command=send_message) send_button.pack(side=tk.RIGHT) chat_frame.pack(expand=True, fill=tk.BOTH) input_frame.pack(expand=True, fill=tk.BOTH) root.mainloop()
时间: 2024-03-28 10:36:18 浏览: 102
import socket
import time
import requests
import re
import tkinter as tk
# 英文输入端
host = "192.168.185.60" # 服务器端可以写"localhost",可以为空字符串"",也为本机IP地址
port = 8888 # 端口号
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)
conn, addr = s.accept()
print('connected with', addr)
def send_message():
trigger = input_text.get()
input_text.delete(0, tk.END)
chat_text.configure(state='normal')
chat_text.insert(tk.END, 'You: ' + trigger + '\n')
chat_text.configure(state='disable')
s.sendall(trigger.encode())
data = s.recv(1024)
data = data.decode()
data2 = {'doctype': 'json', 'type': 'auto', 'i': ''}
data2['i'] = data
r = requests.get("http://fanyi.youdao.com/translate", params=data2) # 访问翻译网站
result = r.json() # 获取翻译内容
t1 = result.get('translateResult')
t2 = t1[0]
t3 = t2[0]['tgt'] # 解析翻译内容
localTime = time.asctime(time.localtime(time.time()))
chat_text.configure(state='normal')
chat_text.insert(tk.END, 'Server: ' + t3 + '\n')
chat_text.insert(tk.END, '\n')
chat_text.configure(state='disable')
if trigger.lower() == 'mint': # 发送Mint结束连接
s.close()
conn.close()
s.close()
root = tk.Tk()
root.title('Chat Window')
chat_frame = tk.Frame(root)
scrollbar = tk.Scrollbar(chat_frame)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
chat_text = tk.Text(chat_frame, wrap=tk.WORD, yscrollcommand=scrollbar.set, state='disable')
chat_text.pack(expand=True, fill=tk.BOTH)
scrollbar.config(command=chat_text.yview)
input_frame = tk.Frame(root)
input_text = tk.Entry(input_frame)
input_text.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)
send_button = tk.Button(input_frame, text='Send', command=send_message)
send_button.pack(side=tk.RIGHT)
chat_frame.pack(expand=True, fill=tk.BOTH)
input_frame.pack(expand=True, fill=tk.BOTH)
root.mainloop()
修正如下:
1. 将t3 = t2[0]改为t3 = t2[0]['tgt'],以正确解析翻译内容。
2. 将result.setdefault('translateResult')改为result.get('translateResult'),以简化代码。
3. 将if trigger.lower() == 'Mint'改为if trigger.lower() == 'mint',以匹配发送Mint结束连接的条件。
阅读全文
相关推荐












import io import re import tkinter import requests import threading import tqdm from pydub import AudioSegment root = tkinter.Tk() root.title('在线视频解析') root.geometry('500x590+550+350') headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0'} ac = tkinter.Listbox(root, width=50, height=20, font=('黑体', 12)) ac.grid(row=2, columnspan=10, sticky="n" + "s" + "w" + "e") def sousuo(): i = b1.get() ac.delete(0, 'end') def extract_music_info(content): p = '|' content = re.sub(p, '', content, flags=re.S) pattern = re.compile('subject.*?href="(.*?)">(.*?)
', flags=re.S) return pattern.findall(content) def search_music(): url = 'https://www.hifini.com/search-' + i + '-1.htm' response = requests.get(url=url, headers=headers) return response.text def update_listbox(music_list): for music in music_list: pppp = music[1] + ":" + music[0] ac.insert('end', pppp) content = search_music() music_list = extract_music_info(content) update_listbox(music_list) def xiazzi(): def download_music(): ppp = ac.get(ac.curselection()) pp = re.search('thread.*?htm', ppp) v = pp.group() url1 = 'https://www.hifini.com/' + v response = requests.get(url=url1, headers=headers) ppp = response.text l2 = re.search('<script>.*?title:..(.*?).,.*?author:.(.*?).,.*?url:..(.*?).,', ppp, flags=re.S) p = 'https://www.hifini.com/' + l2.group(3) response = requests.get(url=p, headers=headers, stream=True) # 设置 stream=True 以启用流式下载 total_size = int(response.headers.get('Content-Length')) music_name = '{}-{}.mp3'.format(l2.group(2), l2.group(1)) progress_bar = tqdm(total=total_size, unit='B', unit_scale=True) # 创建进度条 with open(music_name, 'wb') as f: for data in response.iter_content(chunk_size=1024): progress_bar.update(len(data)) # 更新进度条 f.write(data) progress_bar.close() # 关闭进度条 print(music_name) threading.Thread(target=download_music).start() a1 = tkinter.Label(root, text='音乐下载器', anchor="center", font=('黑体', 24)) a1.grid(row=0, columnspan=10, sticky="n" + "s" + "w" + "e") b1 = tkinter.Entry(root, width=35, font=('黑体', 16), ) b1.grid(row=1, column=3, padx=15) search_button = tkinter.Button(root, text='搜索', command=sousuo) search_button.grid(row=1, column=4) download_button = tkinter.Button(root, text='下载', command=xiazzi) download_button.grid(row=3, column=4) root.mainloop() 报错TypeError: 'module' object is not callable



