繁体   English   中英

具有可点击按钮和创建新按钮选项的站点导航器

[英]Site navigator that has clickable buttons and option to create new button

更新:它在https://url.com时有效,但在url.com时无效

我想制作一个具有可点击按钮的站点导航器,这些按钮将我导航到相应的 url 按钮。 还有另一个按钮,单击它会弹出一个输入字段,我可以输入 url 和按钮的标题。 最初放置的按钮正在工作并导航我到网站。 一切正常,除了当我单击新创建的按钮时它不会将我重定向到网站

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()

一切正常,除了当我单击新创建的按钮时它不会将我重定向到网站

您缺少open_website

def create_button():
    new_url = entry_url.get()
    new_title = entry_title.get()
    open_website(new_url) # Add this.


    :
    :
    :

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM