python - 如何将文本文件放在 tkinter 条目上
问题描述
我正在编写一个代码,在按下按钮后创建一个文本文件。在代码的末尾,在创建 txt 之后,我试图重新打开文件并将所有行粘贴到输入框或带有多行的标签上,任何帮助都是好的。
提前致谢
我知道我的代码很烂,因为我真的是 python 中的菜鸟。但这是我的代码
import tkinter as tk
from tkinter import messagebox
from bs4 import BeautifulSoup
import requests
import os
HEIGHT = 500
WIDTH = 600
def get_anime_function (entry):
if entry == "":
messagebox.showerror("Error", "No hay link que buscar")
else:
url = entry
txt = url.replace('https://animeflv.net/anime/', '')
txt = txt + ".txt"
r = requests.get(url)
for line in r.text.splitlines():
if 'var episodes =' in line:
line = line.replace(' ', '')
line = line.replace('var episodes = ', '')
line = line.replace('[', '')
line = line.replace(']', '')
line = line.replace(';', '')
break
first_char = line[0:3]
first_char = first_char.split(",",maxsplit=1)[0]
first_char = int(first_char)
url = url.replace('/anime/', '/ver/')
for x in range(1, first_char+1):
url2 = url + "-" + str(x)
r2 = requests.get(url2)
html_content = r2.text
soup = BeautifulSoup(html_content, 'lxml')
links = soup.find_all('a', href=True, attrs={'class':'Button Sm fa-download'})
text = str(links)
text = text.replace('[<a class="Button Sm fa-download" href="', '')
text = text.replace('" rel="nofollow" target="_blank">DESCARGAR</a>]', '')
text = text.replace('" rel="nofollow" target="_blank">DESCARGAR</a>, <a class="Button Sm fa-download" href="', '')
text = text.replace('http://ouou.io/s/y0d65LCP?s=', '') #is ouo but the forum dont allow it
text = text.replace('http', '\nhttp')
text = os.linesep.join([s for s in text.splitlines() if s])
text_file = open(txt, "a")
for item in text.split():
if "zippyshare" in item:
n = text_file.write(item + '\n')
text_file.close()
f = open(txt, "r")
lower_entry['text'] = f
f.close()
root = tk.Tk()
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
#background_image = tk.PhotoImage(file='background.png')
#background_label = tk.Label(root, image=background_image)
#background_label.place(relwidth=1, relheight=1)
frame = tk.Frame(root)
frame.place(relx = 0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
label = tk.Label(frame, text="Link de AnimeFLV", anchor='w')
label.place(relx = 0, rely=0, relwidth=0.65, relheight=0.4)
entry = tk.Entry(frame, font=40, justify='left')
entry.place(rely = 0.4, relwidth=0.65, relheight=0.5)
button = tk.Button(frame, text="Ejecutar", font=60, command=lambda: get_anime_function(entry.get()))
button.place(relx = 0.7, relwidth=0.3, relheight=1)
lower_frame = tk.Frame(root)
lower_frame.place(relx = 0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')
lower_entry = tk.Entry(lower_frame, font=60)
lower_entry.place(relwidth=1, relheight=1)
root.mainloop()
解决方案
欢迎编程并继续前进!你会变得更好,不要对自己太苛刻!这是一个读取文本文件并在标签上显示内容的示例。
from tkinter import *
root = Tk()
with open("SomeTextFile.txt", "r") as f:
Label(root, text=f.read()).pack()
root.mainloop()
推荐阅读
- pytorch - Nsight Compute 无法分析 Waveglow(PyTorch 应用程序)
- javascript - 将 curl 转换为 fetch
- java - 使用 jpa 的 Spring 数据在 SQL Server 中存储 utf-8 字符串的更方便的方法
- java - 在jooq中设置NULL值
- node.js - IBM watson 助手 Nodejs 中未经授权的问题
- kubernetes - 如何设置 Kubernetes 持久化环境变量
- eclipse - 新的运行时配置无法打开并出现错误无法写入核心转储。核心转储已被禁用
- istio - 两个工作节点之间的 VPN
- openmaptiles - 是否可以以较低的缩放级别显示铁路基础设施?
- java - 如何让扫描仪理解我写的数量?