-
I am making a basic network with HTML files being sent from servers to clients and the HTML is successfully transferred as the HTML file is in the directory of the CLIENT.py code
-
However when I attempt to show this code on the tkinter window it freezes
import tkinter as tk
from tkinter import ttk
from tkhtmlview import HTMLLabel
from WEBBROWSER import get_html
class SimpleBrowser:
def __init__(self, root):
self.root = root
self.root.title("Simple Browser")
self.root.geometry("800x600")
# Create the main frame
self.main_frame = ttk.Frame(self.root, padding="10 10 10 10")
self.main_frame.grid(row=0, column=0, sticky=(tk.N, tk.S, tk.E, tk.W))
self.root.columnconfigure(0, weight=1)
self.root.rowconfigure(0, weight=1)
# Create the search bar
self.url_var = tk.StringVar()
self.search_bar = ttk.Entry(self.main_frame, textvariable=self.url_var, width=60)
self.search_bar.grid(row=0, column=0, padx=5, pady=5, sticky=(tk.W, tk.E))
self.search_button = ttk.Button(self.main_frame, text="Search", command=self.search_html)
self.search_button.grid(row=0, column=1, padx=5, pady=5)
# Create the HTML viewer
self.html_viewer = HTMLLabel(self.main_frame, html="<h1>Welcome to Simple Browser</h1>")
self.html_viewer.grid(row=1, column=0, columnspan=2, padx=5, pady=5, sticky=(tk.N, tk.S, tk.E, tk.W))
# Add row/column configuration
self.main_frame.columnconfigure(0, weight=1)
self.main_frame.columnconfigure(1, weight=0)
self.main_frame.rowconfigure(1, weight=1)
# GETTING AND SHOWING HTML =====================> ERROR IS PROBABLY HERE
def search_html(self):
url = self.search_bar.get()
# Fetch HTML content and save it to a file
get_html(url)
# GET HTML FILE CONTENT
html_file = open("WEBSITE.html", "r")
html_file_cont = html_file.read()
html_file.close()
print(html_file_cont)
# Display HTML file directly
self.html_viewer.set_html(html_file_cont)
if __name__ == "__main__":
root = tk.Tk()
app = SimpleBrowser(root)
root.mainloop()
HTML FILE
<!DOCTYPE html>
<html lang="en">
<head>
<title>TEST 1</title>
</head>
<body>
<h1>HELLOW WORLD TEST1</h1>
</body>
</html>
I changed the code self.html_viewer.set_html(html_file_cont) on the 5th last line to self.html_viewer.set_html(“<h 1>CHANGED</h 1>”) to see if the error came from displaying a HTML file in the first place but it still freezes
- I added a space between H1 as stackoverflow was displaying changed as a header I didnt write it like that in my code
Muhammad Ali is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.