简体   繁体   English

Tkinter修改OptionMenu的背景?

[英]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. 美好的一天,我不是一名程序员,并且对与Python相关的任何事情都感到恐惧-因此,如果这有点荒谬,我深表歉意。

Trying to create a pretty basic UI with some text entry, labels, and some OptionMenus (tkinter). 尝试使用一些文本输入,标签和一些OptionMenus(tkinter)创建一个非常基本的UI。

Am attempting to change the background color of the OptionMenu widget using 我正在尝试使用以下方法更改OptionMenu小部件的背景颜色

OptionMenu.configure(bg="blue")

Unfortunately, I get the error 不幸的是,我得到了错误

TypeError: configure() missing 1 required positional argument: 'self' TypeError:configure()缺少1个必需的位置参数:“ 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. 哦,如果要紧,我正在使用Python 3.6和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. 如Jason在上面的评论中所述,您需要在要配置的对象上使用.configure()而不是类。

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: 另外,您可以使用属性activebackground来配置背景的颜色,这是在首次单击`OptionMenu之后,可以像下面这样进行:

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 . 如果您想进一步提高一致性,则实际上也可以通过调用对象的键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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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