pop-os / cosmic-comp

Compositor for the COSMIC desktop environment
GNU General Public License v3.0
489 stars 93 forks source link

Python tkinter menu cascades don't appear #971

Open ragardner opened 3 weeks ago

ragardner commented 3 weeks ago

Hello,

On an up to date cosmic release (as of writing) using Pop OS 22.04 menu cascades from a tkinter app menu bar are not appearing

  1. Running the following code will show a tkinter window with a menu bar at the top with one item named "File"
  2. Clicking on the "File" menu will not show a cascade on cosmic, but it will on gnome

Also, the window centers on gnome but not on cosmic. This seems to be related to the tk.Toplevel function geometry() not working correctly - I am not sure if this is relevant to cosmic but I could create a separate issue for this if that's best

Kind regards and thank you

import tkinter as tk

def center(
    toplevel,
    new_w=None,
    new_h=None,
):
    toplevel.update_idletasks()
    screen_w = toplevel.winfo_screenwidth()
    screen_h = toplevel.winfo_screenheight()

    if new_w is not None and new_w >= screen_w:
        new_w = screen_w - 20
    if new_h is not None and new_h >= screen_h:
        new_h = screen_h - 20

    if new_w and new_h:
        size = (new_w, new_h)
    else:
        size = tuple(map(int, toplevel.geometry().split("+")[0].split("x")))

    x = screen_w / 2 - size[0] / 2
    y = screen_h / 2 - size[1] / 2

    geom = f"{size[0]}x{size[1]}+{int(x)}+{int(y)}"

    print(screen_w, screen_h)
    print(geom)
    toplevel.geometry(geom)

    return x, y

class demo(tk.Tk):
    def __init__(self):
        super().__init__()
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)
        self.label = tk.Label(
            self,
            text="      App       ",
            font=("Arial", 20, "bold"),
        )
        self.label.grid(
            row=0,
            column=0,
            sticky="nswe",
        )
        self.menu_bar = tk.Menu(self, tearoff=0)
        self.file_menu = tk.Menu(
            self.menu_bar,
            tearoff=0,
        )
        self.file_menu.add_command(
            label="Option 1",
            command=self.one,
        )
        self.file_menu.add_command(
            label="Option 2",
            command=self.two,
        )
        self.file_menu.add_command(
            label="Reset",
            command=self.reset,
        )
        self.menu_bar.add_cascade(
            label="File",
            menu=self.file_menu,
        )
        self.config(menu=self.menu_bar)
        center(self, new_w=350, new_h=350)

    def one(self, event=None):
        self.label.config(text="      One       ")

    def two(self, event=None):
        self.label.config(text="      Two       ")

    def reset(self, event=None):
        self.label.config(text="      App       ")

app = demo()
app.mainloop()