【问题标题】:Why am I not able to visualise background colours for buttons in Tkinter Python on a Mac?为什么我无法在 Mac 上可视化 Tkinter Python 中按钮的背景颜色?
【发布时间】:2019-09-12 04:48:11
【问题描述】:

我在 Tkinter GUI 中有六个不同的按钮,每个按钮都有不同的颜色。我可以在 Windows 10 上使用 anaconda 来可视化它们。但我无法在 Mac OS 上使用相同的 Anaconda-Navigator 来可视化这些按钮。我不知道为什么?

按钮的python代码如下:

b1 = Button(root, text="Elbow Method", command=plot_elbow, bg="green", fg="white").pack(side = LEFT)
b2 = Button(root, text="K-Means Clustering", command=plot_kmeans, bg="blue", fg="white").pack(side = LEFT)
b3 = Button(root, text="Batsmen who scored 4 or more Hundreds", command=plot_hundreds, bg="#D35400", fg="white").pack(side = LEFT)
b4 = Button(root, text="Runs Scored by Various Players", command=plot_runs, bg="#117A65", fg="white").pack(side = LEFT)
b5 = Button(root, text="Best Batsmen", command=plot_best_batsmen, bg="#34495E", fg="white").pack(side = LEFT)
b6 = Button(root, text="Stop", command=root.destroy, bg="red", fg="white").pack(side = BOTTOM)

我的预期输出是 6 个按钮,每个按钮都有不同的背景颜色。然而,无论我使用什么颜色,它都会显示 6 个按钮,所有这些按钮都具有白色背景。

那么,谁能帮我解决这个问题?

【问题讨论】:

  • 在 Mac 上,您无法更改按钮的背景颜色。但是您可以通过从 LabelCanvas 创建自定义按钮类来实现。
  • @Saad,为什么会这样?你绝对确定吗?当我放置标签而不是按钮时,我在 Windows 中收到以下错误:File "C:\Users\Vivek\Anaconda3\lib\tkinter\__init__.py", line 2299, in __init__ (widgetName, self._w) + extra + self._options(cnf)) TclError: unknown option "-command"
  • Vivek,你不能在标签中传递命令。我的意思是创建一个继承自LabelCanvas 的自定义类,然后使用bind 方法调用该函数。如果您仍然不明白,我可以发布答案,but first check this post
  • 你不能这样做的原因仅仅是因为 Apple 不允许,所以该平台上的应用程序保持一致的外观。

标签: python-3.x macos tkinter anaconda


【解决方案1】:

我在“How to change the foreground or background colour of a Tkinter Button on Mac OS X?”找到了这个问题的答案

只需将命令 bg="color" 更改为 highlightbackground="color" 在 Mac OS 上对我有用。

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 2020-07-15
    • 2020-10-09
    • 2022-07-07
    • 2022-01-02
    • 1970-01-01
    • 2021-07-24
    相关资源
    最近更新 更多