简体   繁体   中英

Tkinter Modify OptionMenu background?

Good day, I'm not a programmer, and certainly horrible with anything related to Python - so I apologize if this is kind of ridiculous.

Trying to create a pretty basic UI with some text entry, labels, and some OptionMenus (tkinter).

Am attempting to change the background color of the OptionMenu widget using

OptionMenu.configure(bg="blue")

Unfortunately, I get the error

TypeError: configure() missing 1 required positional argument: 'self'

From what I've read, people get this when they're missing something in a class or not instantiated the class. Not only don't I have a clue what that means, but I'm not creating any classes. At least not that I'm aware of.

Any idea why I'd be getting that error and how to fix it? I know I've got MUCH bigger fish to fry (like how to use the values after they are entered in the text boxes), but baby steps...

Oh, and if it matters I'm using Python 3.6 and Spyder. Thanks!

As was stated by Jason in the comments above, you need to use .configure() on the object you want to configure not the class.

See below:

from tkinter import *

root = Tk()

var = StringVar(root)
var.set(1)

array = [1, 2, 3]

option = OptionMenu(root, var, *array)
option.configure(background="white")

option.pack()

Additionally you can use the attribute activebackground to configure the colour of the background after the initial click on the `OptionMenu, this can be done like the below:

from tkinter import *

root = Tk()

var = StringVar(root)
var.set(1)

array = [1, 2, 3]

option = OptionMenu(root, var, *array)
option.configure(background="white", activebackground="white")

option.pack()

If you wanted to go even further for consistency's sake you can actually configure the "dropdown menu" background colour too by calling the object's key menu . This can be done like the below:

from tkinter import *

root = Tk()

var = StringVar(root)
var.set(1)

array = [1, 2, 3]

option = OptionMenu(root, var, *array)
option.configure(background="white", activebackground="white")
option["menu"].configure(bg="white")

option.pack()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM