UPDATE: It is working when https://url.com
but it doesnt when url.com
I want to make a site navigator that has clickable buttons which navigate me to the corresponding url of button. And also has another button that when clicked it will pop an input field that i can type the url and the title of the button. A buttons that originaly placed are working and navigate me to website. Everythings works except when i click the new created button it doesnt redrict me to website
import tkinter as tk
import webbrowser
def open_website(url):
webbrowser.open(url)
def add_button():
# Hide the "Add Website" button
add_button.pack_forget()
# Display the input fields for URL and title
label_url.pack(anchor="center", padx=10, pady=5)
entry_url.pack(anchor="center", padx=10, pady=5)
label_title.pack(anchor="center", padx=10, pady=5)
entry_title.pack(anchor="center", padx=10, pady=5)
# Show the "Create Button" button
create_button.pack(anchor="center", padx=10, pady=5)
def create_button():
new_url = entry_url.get()
new_title = entry_title.get()
if new_url and new_title:
button = tk.Button(root, text=new_title, command=lambda url=new_url: open_website(url), **button_style)
button.pack(anchor="center", padx=10, pady=5)
entry_url.delete(0, tk.END)
entry_title.delete(0, tk.END)
# Hide the input fields and "Create Button" button after adding the new button
label_url.pack_forget()
entry_url.pack_forget()
label_title.pack_forget()
entry_title.pack_forget()
create_button.pack_forget()
# Show the "Add New Website" button again
add_button.pack(anchor="center", padx=10, pady=5)
root = tk.Tk()
root.title("Navigator") # Set the title of the window
root.configure(bg="black") # Set the background color to black
def open_google():
open_website("https://www.google.com")
def open_facebook():
open_website("https://www.facebook.com")
def open_twitter():
open_website("https://www.twitter.com")
button_style = {
"bg": "black", # Set button background color to black
"fg": "white", # Set button foreground (text) color to white
"activebackground": "darkred", # Set background color when button is clicked or activated
"activeforeground": "white", # Set foreground color when button is clicked or activated
"highlightbackground": "black", # Set border color to black
"highlightcolor": "black", # Set border color to black
"highlightthickness": 0, # Remove the highlight border
"borderwidth": 0, # Remove the default button border
"font": ("Arial", 12, "bold") # Set the button font and size
}
button1 = tk.Button(root, text="Google", command=open_google, **button_style)
button1.pack(anchor="center", padx=10, pady=5)
button2 = tk.Button(root, text="Facebook", command=open_facebook, **button_style)
button2.pack(anchor="center", padx=10, pady=5)
button3 = tk.Button(root, text="Twitter", command=open_twitter, **button_style)
button3.pack(anchor="center", padx=10, pady=5)
# Entry fields for new website URL and title
label_url = tk.Label(root, text="Enter URL:", fg="white", bg="black")
entry_url = tk.Entry(root, width=30)
label_title = tk.Label(root, text="Enter Title:", fg="white", bg="black")
entry_title = tk.Entry(root, width=30)
# Create Button button
create_button = tk.Button(root, text="Create Button", command=create_button, **button_style)
# Add Website button
add_button = tk.Button(root, text="Add New Website", command=add_button, **button_style)
add_button.pack(anchor="center", padx=10, pady=5)
root.mainloop()
Everythings works except when i click the new created button it doesnt redrict me to website
You are missing open_website
.
def create_button():
new_url = entry_url.get()
new_title = entry_title.get()
open_website(new_url) # Add this.
:
:
:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.