【问题标题】:Changing color of buttons in tkinter works on Windows but not Mac OSX更改 tkinter 中按钮的颜色适用于 Windows,但不适用于 Mac OSX
【发布时间】:2018-12-03 12:21:03
【问题描述】:

编辑:我在 Windows 上尝试过,它可以工作,我猜这是 OSX 错误?

以下代码由于某种原因无法正常工作,并且按钮保持白色

z = Button(frame, text="Nothing Scheduled", bg = "blue" command=lambda ..., width=15)
z.grid(row=x, column=1)

但是,由于某些原因,设置标签的背景可以正常工作,如下所示

Label(frame, text=times[x], bg="blue").grid(row=x, column=0)

所以我最终得到了以下 GUI,如下所示

我知道this 帖子中详述的错误,但是我没有使用 ttk,所以我认为这不适用于这里。我只是在 Python 3.6.4 中使用from tkinter import *,但是我在 Mac OSX 上

我也尝试过z.config(bg="blue")z["bg"]="blue",但都失败了。

【问题讨论】:

    标签: python macos tkinter


    【解决方案1】:

    对于python3,试试

    pip3 install tkmacosx
    

    那么它应该与这个一起工作:

    from tkmacosx import Button as button
    B1 = button(frame, text = 'Hello!', bg = 'black', fg = 'white',command = testing)
    

    【讨论】:

      【解决方案2】:

      在 tcl/tk wiki 的 page 中列出了一些与 Mac 相关的问题以及标签和按钮背景的颜色。例如:

      ...

      Mac OS X 背景颜色不应该是白色,而应该是#ececec。由于 winfo rgb 在 mac 颜色上无法正常工作,因此很难获得正确的默认颜色。

      ...等等

      Saludos!,

      【讨论】:

        【解决方案3】:

        我在 osx 10.14.3 上遇到了同样的问题,然后我用 ttk 按钮替换,它就可以了!

        from tkinter import ttk
        b1 = ttk.Button(root, text="start", width=15, command=begin)
        

        【讨论】:

          【解决方案4】:

          使用tkmacosx 库,它允许您在 MacOSX 中更改按钮颜色。

          安装

          pip install tkmacosx
          

          示例

          from tkinter import *
          from tkmacosx import Button
          
          root = Tk()
          root.geometry("200x200")
          B1 = Button(root, text='Button', bg='red')
          B1.grid(row=0, column=1)
          
          root.mainloop()
          

          结果

          To see the result click here

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-07-27
            • 1970-01-01
            • 2016-05-23
            • 2012-02-08
            • 2014-08-11
            • 1970-01-01
            • 2017-08-20
            • 2016-11-07
            相关资源
            最近更新 更多