This repository has been archived on 2021-12-22. You can view files and clone it, but cannot push or open issues or pull requests.
tkGemini/main.py

70 lines
2.4 KiB
Python

import tkinter as tk
import tkinter.messagebox
import tkinter.simpledialog
import gemini
# See https://tildegit.org/solderpunk/gemini-demo-1/src/branch/master/gemini-demo.py
class Application():
def __init__(self):
self.root = tk.Tk()
self.nav_bar = NavBar(self)
self.content = Content(self)
self.root.mainloop()
self.current_URL = "about:home"
def updateContent(self):
# Get url asked
self.current_URL = self.nav_bar.getURL()
# New request
r = gemini.Request(self.current_URL)
status = r.makeRequest()
# The server asked for user input
if status[0] == "1":
user_input = tkinter.simpledialog.askstring("User input", "The server asked for " + r.meta + ".")
self.nav_bar.URL_var.set(self.current_URL + "?" + user_input)
self.updateContent()
# If server asked for redirection
elif status[0] == "3":
# Ask to user
confirm = tkinter.messagebox.askyesno("Server redirection", "This server ask for redirection to " + r.meta + ". Follow ?")
# If he accepted
if confirm:
self.nav_bar.URL_var.set(r.meta)
self.updateContent()
# The server asked for client certificate, unsupported
elif status[0] == "6":
tkinter.messagebox.showerror("Client certificate required", "Error " + status + " : " + r.meta)
else:
self.content.setContent(r.content)
class NavBar():
def __init__(self, parent):
self.parent = parent
self.URL_var = tk.StringVar()
self.root = tk.Frame(self.parent.root)
self.root.grid(column=0, row=0)
self.URL_bar = tk.Entry(self.root, textvariable=self.URL_var)
self.URL_var.set("about:home")
self.URL_bar.grid(column=0, row=0)
self.go_button = tk.Button(self.root, text='Go !', command=self.parent.updateContent)
self.go_button.grid(column=1, row=0)
def getURL(self):
return self.URL_var.get()
class Content():
def __init__(self, parent):
self.parent = parent
self.root = tk.Frame(self.parent.root)
self.root.grid(column=0, row=1)
self.line = tk.Label(self.root, text="My home page")
self.line.grid(column=0, row=0)
def setContent(self, new_content):
self.line["text"] = new_content
app = Application()