10

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

10 Rep
  • 2,217
  • 7
  • 19
  • 33
coderoftheday
  • 1,987
  • 4
  • 7
  • 21

2 Answers2

5

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!

    10 Rep
    • 2,217
    • 7
    • 19
    • 33
    • 3
      Consider bind an event `` to make it disappear. – jizhihaoSAMA Jun 16 '20 at 23:57
    • @jizhihaoSAMA Okay, how do I do that? How do I use ``````. – 10 Rep Jun 17 '20 at 00:03
    • FYI, I know the menu is buggy, I am working on fixing it. – 10 Rep Jun 17 '20 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 Jun 17 '20 at 19:19
    • @MinionJim Thanks mate!! It took me a day, and I will improve on the widget. – 10 Rep Jun 17 '20 at 19:28
    -3

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

    10 Rep
    • 2,217
    • 7
    • 19
    • 33