簡體   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