11

I'm trying to get rid of the white border around the OptionMenu.

What I tried

I changed the colour to red, but there is still a white border around it.

Can anyone help?

enter image description here

Here's the code:

from tkinter import *
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('500x500')
var = StringVar()
option = ttk.OptionMenu(root,var,'1','2','3')
option["menu"].config(bg="red")
option.pack()
root.mainloop()

Also, is there a way to change the colour of the OptionMenu trigger box (In the red Circle)? enter image description here

18
  • Have you tried setting the borderwidth to zero? Commented Jun 8, 2020 at 19:47
  • I just done option["menu"].config(bg="red",borderwidth=0) nothing changed unfortunately, Commented Jun 8, 2020 at 19:49
  • Could not reproduce with the given code example
    – rizerphe
    Commented Jun 12, 2020 at 14:12
  • The window is not blue, but the menu is red demonstrating the grey border.
    – Minion Jim
    Commented Jun 12, 2020 at 16:57
  • Have you considered writing your own option menu?
    – Mike - SMT
    Commented Jun 16, 2020 at 15:08

2 Answers 2

6
+100

As stated in the comments by @Mike-SMT,

Have you considered writing your own option menu?

This, to me, seems to be the only way to get an OptionMenu without having that irritating grey border.

Here is my attempt at it:

import tkinter as tk
root = tk.Tk()
root.geometry('500x500')

class custom_option_menu(tk.Tk):

    def down(self, *menu_items):
        if self.button["text"] == "↓":
            self.frame.place(x = self.x + (len(self.first) * 13)/2, y = self.y + 50, anchor = "center")
            self.button.config(text = "↑")

        elif self.button["text"] == "↑":
            self.frame.place_forget()
            self.button.config(text = "↓")

    def __init__(self, master, first, bg, *menu_items):

        self.master = master
        self.first = first
        self.menu_items = menu_items
        self.bg = bg
        self.frame = tk.Frame(master, height = 100, width = 100)
        self.otherframe = tk.Frame(master, height = 10, width = len(first) * 13)
        self.label = tk.Label(self.otherframe, text = str(first))
        self.button = tk.Button(self.otherframe, text = "↓", command = lambda: self.down(), relief= "flat")
        def save_var(event = "<Button-1>"):
            print(event.widget["text"])
        for i in range(len(self.menu_items)):
            self.frame.config(bg = self.bg)
            self.option = tk.Button(self.frame, text = self.menu_items[i], relief = "flat", bg = self.bg, textvariable = int(i))
            self.option.pack()
            self.option.bind("<Button-1>", save_var)





    def put(self, x, y):
        self.x = x
        self.y = y
        self.button.pack(side = "right")
        self.label.pack()
        self.frame.place(x = x + (len(self.first) * 13)/2, y = y + 50, anchor = "center")

        self.frame.place_forget()
        self.otherframe.place(x = x + (len(self.first) * 13)/2, y = y, anchor = "center")

nice = custom_option_menu(root, "o000000000000000", "blue", "First", "second", "Third")
nice.put(100, 200)
root.mainloop()

Sadly I couldn't get the default geometry managers to work for this, so I created .put(). It's just the x and y coordinates.

The arguments to the class custom_option_menu go as follows:

  • The first argument is the parent widget.
  • The second argument is the text on the OptionMenu.
  • The third argument is the background color for the options.
  • The remaining arguments are the options.
  • To open the menu, click the down arrow.

    I hope this is what you were looking for!

    5
    • 3
      Consider bind an event <Leave> to make it disappear. Commented Jun 16, 2020 at 23:57
    • @jizhihaoSAMA Okay, how do I do that? How do I use <Leave>.
      – 10 Rep
      Commented Jun 17, 2020 at 0:03
    • FYI, I know the menu is buggy, I am working on fixing it.
      – 10 Rep
      Commented Jun 17, 2020 at 14:07
    • 1
      @TheMaker, I appreciate the effort you have put into this answer, though it is quite sad that it is built on assumptions (character width of 13, button height of 50, text of main button not changing, etc.). Despite this, it did indeed "remove the grey border around the Menu part of the OptionMenu" so I have awarded you the bounty.
      – Minion Jim
      Commented Jun 17, 2020 at 19:19
    • @MinionJim Thanks mate!! It took me a day, and I will improve on the widget.
      – 10 Rep
      Commented Jun 17, 2020 at 19:28
    -3

    Try option["highlightthickness"]=0. That should remove the border.

    6
    • 2
      This removes the border around the button (which would help with the part about setting the bg of the trigger box), not the border on the menu.
      – Minion Jim
      Commented Jun 8, 2020 at 20:20
    • Thanks for that, so you don't know how to solve the one before? Commented Jun 8, 2020 at 20:50
    • This configuration only works on tk.OptionMenu not ttk.OptionMenu. The OP is using ttk here.
      – Mike - SMT
      Commented Jun 16, 2020 at 17:55
    • @Mike-SMT I tried this on tk.OptionMenu. It still doesn't work.
      – 10 Rep
      Commented Jun 16, 2020 at 18:33
    • 1
      @TheMaker I was only saying that the highlightthickness is only available to tk not ttk. I know it doesn't solve your issue.
      – Mike - SMT
      Commented Jun 16, 2020 at 18:45

    Not the answer you're looking for? Browse other questions tagged or ask your own question.