[Python] VS Code and/or Python tkinter not running properly. Colors and buttons cutoff. Issue...

Discussão em 'Python' iniciado por Stack, Fevereiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    For the past few days, my Visual Studio Code and python has been a bit weird. In a course I am taking, we have been coding with tkinter and everything has been fine until about last week. When programs are ran, colors are not showing properly and buttons are being cutoff. I will include an example of an assignment that we did. The code worked perfectly fine on my PC and on a friends laptop, but on my macbook it does not.

    It could be a code issue, but it's seeming to be more of an issue with VS or my python. Github Copilot tries to change the code to fit the buttons and fix the colors, but that just makes it look even more messy.

    So far, I've tried reinstalling VS and upgrading tkinter and python. I will attach the code and an example of what the output should look like vs what it does look like.

    import tkinter as tk

    from tkinter import messagebox

    # add contact
    def add_contact():
    contact = entry.get()
    if contact:
    listbox.insert(tk.END, contact)
    entry.delete(0, tk.END)
    messagebox.showinfo("Contact Added", "New contact has been added successfully!")
    else:
    error_label.config(text="Please enter a contact name")

    # select contact
    def on_contact_select(evt):
    selected_indices = listbox.curselection()
    if selected_indices:
    selected_contact = listbox.get(selected_indices[0])
    selected_label.config(text=f"Selected Contact: {selected_contact}")
    else:
    selected_label.config(text="Selected Contact:")

    # delete contact
    def delete_contact(evt):
    selected_indices = listbox.curselection()
    if selected_indices:
    listbox.delete(selected_indices[0])
    selected_label.config(text="Selected Contact:")
    clicked_label.config(text="Button Clicked: Delete Contact")
    else:
    error_label.config(text="No contact selected to delete")

    # clear all contacts
    def clear_all_contacts(evt):
    listbox.delete(0, tk.END)
    selected_label.config(text="Selected Contact:")
    clicked_label.config(text="Button Clicked: Clear All")

    root = tk.Tk()
    root.title("Contact List Manager")
    root.geometry("500x400+500+100")

    frame = tk.Frame(root, bg="light green", width=350, height=50)
    frame.pack()
    frame.grid_propagate(False)

    entry = tk.Entry(frame, width=30)
    entry.grid(row=0, column=0)

    add_button = tk.Button(frame, text="Add Contact", command=add_contact)
    add_button.grid(row=0, column=1)

    selected_label = tk.Label(root, text="Selected Contact:")
    selected_label.pack()

    clicked_label = tk.Label(root, text="Button Clicked:")
    clicked_label.pack()

    listbox = tk.Listbox(root, height=8, width=50)
    listbox.pack()
    listbox.bind("<<ListboxSelect>>", on_contact_select)

    # error message
    error_label = tk.Label(root, text="", fg="red")
    error_label.pack()

    # buttons
    button_frame = tk.Frame(root, bg="light blue", width=400, height=50)
    button_frame.pack()
    button_frame.grid_propagate(False)

    delete_button = tk.Button(button_frame, text="Delete Contact", width=15)
    delete_button.grid(row=0, column=0)
    delete_button.bind("<Button-1>", delete_contact)

    clear_button = tk.Button(button_frame, text="Clear All", width=15)
    clear_button.grid(row=0, column=1)
    clear_button.bind("<Button-1>", clear_all_contacts)

    quit_button = tk.Button(button_frame, text="Quit", width=15, command=root.quit)
    quit_button.grid(row=0, column=2)

    root.mainloop()


    Image of what output should look like (also what the code shows on my pc)

    Image of what my laptop shows

    Continue reading...

Compartilhe esta Página