I am trying to make a GUI with Tkinter that allows you to enter either a URL or an ID.
Example URL: https://www.fanfiction.net/s/10030860/1/The-Final-Battle Example ID: 10030860
As you can see, the ID is embedded in the URL. I want to have two entries, one for entering a URL and one for entering an ID. If a user fills in the URL box, the ID is automatically generated. (If I put the example URL in the URL entry box, I want the example ID in the ID box automatically generated and vice versa) If a user fills in the ID box, the URL is automatically generated.
More examples: (The brackets are pretending to entry boxes)
URL: [https://www.fanfiction.net/s/10030860/1/The-Final-Battle] <-- If I fill this in
ID: [10030860] <-- Python fills this in for me
URL: [https://www.fanfiction.net/s/10030860/1/The-Final-Battle] <-- Python fills this in for me
ID: [10030860] <-- If I fill this in
Here is my code so far:
import tkinter as tk
from tkinter import ttk
# Define a function to autofill in the URL and ID entries
def autofill_id_url():
fanfic_url.set("https://www.fanfiction.net/s/" + fanfic_id.get() + "/1/")
root.after(100, autofill_id_url)
# Root window
root = tk.Tk()
# Define the labeled frame where we input stuff
input_frame = tk.LabelFrame(master=root, text="Input")
input_frame.grid(row=0, column=0, padx=1, pady=1, rowspan=2, sticky=tk.NS)
# Label for entering URL
ttk.Label(master=input_frame, text="URL:").grid(row=0, column=0, padx=1, pady=1)
# Entry field for URL
fanfic_url = tk.StringVar()
url_entry = ttk.Entry(master=input_frame, textvariable=fanfic_url)
url_entry.grid(row=0, column=1, padx=1, pady=1)
# Label for entering ID
ttk.Label(master=input_frame, text="ID:").grid(row=1, column=0, padx=1, pady=1)
# Entry field for ID
fanfic_id = tk.StringVar()
id_entry = ttk.Entry(master=input_frame, textvariable=fanfic_id)
id_entry.grid(row=1, column=1, padx=1, pady=1)
# Start callback functions
autofill_id_url()
# Start GUI event loop
root.mainloop()
I have the part when you fill in the ID, the URL is generated automatically. But I have no idea how to make it when you fill in the URL box, you get the ID box filled in for you.
Thanks in advance.