get rid of white border around option menu

我怕爱的太早我们不能终老 提交于 2020-08-27 02:18:16

问题


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?

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)?


回答1:


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!




    回答2:


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



    来源:https://stackoverflow.com/questions/62269892/get-rid-of-white-border-around-option-menu

    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!