摆脱选项菜单周围的白色边框
get rid of white border around option menu
我正在尝试去除 OptionMenu
周围的白色边框。
我试过的
我把颜色改成了红色,但是周围还是有白边。
有人能帮忙吗?
代码如下:
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()
此外,有没有办法改变 OptionMenu
触发框(红色圆圈中)的颜色?
尝试option["highlightthickness"]=0
。那应该删除边框。
如@Mike-SMT 的评论所述,
Have you considered writing your own option menu?
对我来说,这似乎是获得 OptionMenu
而没有令人讨厌的灰色边框的唯一方法。
这是我的尝试:
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()
遗憾的是我无法让默认的几何管理器为此工作,所以我创建了 .put()
。它只是 x 和 y 坐标。
class custom_option_menu
的参数如下:
第一个参数是父控件。
第二个参数是OptionMenu
上的文字。
第三个参数是选项的背景颜色。
剩下的参数就是选项。
要打开菜单,请单击向下箭头。
希望这就是您要找的!
我正在尝试去除 OptionMenu
周围的白色边框。
我试过的
我把颜色改成了红色,但是周围还是有白边。
有人能帮忙吗?
代码如下:
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()
此外,有没有办法改变 OptionMenu
触发框(红色圆圈中)的颜色?
尝试option["highlightthickness"]=0
。那应该删除边框。
如@Mike-SMT 的评论所述,
Have you considered writing your own option menu?
对我来说,这似乎是获得 OptionMenu
而没有令人讨厌的灰色边框的唯一方法。
这是我的尝试:
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()
遗憾的是我无法让默认的几何管理器为此工作,所以我创建了 .put()
。它只是 x 和 y 坐标。
class custom_option_menu
的参数如下:
OptionMenu
上的文字。 要打开菜单,请单击向下箭头。
希望这就是您要找的!