0

I'm creating a simple gui application in python. I know the existing drop down menu option in tkinter but I want it to appear on the title bar. Similar to what you can find on the gnome-calculator in ubuntu18.04 (selecting between modes). How to achieve this using python3 tkinter?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Joe Men
  • 3
  • 1
  • You have to prevent the window manager from drawing a title bar and use tkinter facilities to fake one (if you think of it, this is what Gnome applications tipically do). – gboffi Jul 31 '19 at 09:11

1 Answers1

1

You will need to create a frameless window using self.overrideredirect(True) and put a widget representing the title bar on the top of the window. Put your dropdown into that widget. Simple example:

import tkinter
from tkinter import ttk

class App(tkinter.Tk):

    def __init__(self):
        tkinter.Tk.__init__(self)
        self.title("Example")
        self.overrideredirect(True)
        self.title_bar = ttk.Combobox(values=["Mode 1", "Mode 2"])
        self.title_bar.set("Mode 1")
        self.title_bar.state(["readonly"])
        self.title_bar.pack()

app = App()
app.mainloop()

Note that in this example the window is not visible in the taskbar. See Tkinter, Windows: How to view window in windows task bar which has no title bar?

Maximouse
  • 4,170
  • 1
  • 14
  • 28
  • When I execute your code the following error is raised: `_tkinter.TclError: Invalid state name r` from the line `self.title_bar.state("readonly")` — I've got the same behavior with the system Python and with Anaconda's one. – gboffi Jul 31 '19 at 09:37
  • @gboffi Thank you for telling that. I'll fix the code. – Maximouse Jul 31 '19 at 09:42
  • I tried to execute your code because I fear that your window interacts BADLY with the wm, have you checked that? – gboffi Jul 31 '19 at 09:50
  • Now that you have "fixed" your code I can tell that your window DOES interact badly with the window manager. – gboffi Jul 31 '19 at 09:51
  • This is just a minimal example. It could have a lot more features. – Maximouse Jul 31 '19 at 09:53
  • This is a wrong example. – gboffi Jul 31 '19 at 09:53
  • @gboffi I added a link to a related question that explains how to make the window appear in the taskbar. – Maximouse Jul 31 '19 at 10:52