【问题标题】:Background Button Color is not working properly MACOS背景按钮颜色无法正常工作 MACOS
【发布时间】:2023-02-09 19:13:16
【问题描述】:

我正在尝试为学校项目制作一个简单的 GUI。我是使用 tkinter 的新手。我曾经使用 pygame 创建我的 GUI,这对于定制化非常有用,但并不是为了效率哈哈。

我正在为 /graphs 文件夹中的每个文件创建一个按钮。在这段代码的第 42 行中,在我看来我无法更改按钮的背景颜色。这是我在使用 tkinter 时经常遇到的问题。我不知道这是我做错了什么,还是我使用的框架有问题。

我正在尝试将 bg 颜色设为红色。

我正在使用 MACOS,我知道 tkinter 和 mac 有一些复杂性,但我不能使用 tkmacosx,因为我需要这个项目也可以在 windows 和 linux 上运行。

感谢您的帮助,如果您对我没有应用的 tkinter 的常见做法有任何建议,或者如果您有解决我的问题的方法,请不要犹豫!

这是输出和代码 Output

import tkinter as tk
import os

# colors
SILVER = "#BFACAA"
BLACK = "#02020A"
OXFORD_BLUE = "#05204A"
WISTERIA = "#B497D6"
LAVENDER = "#E1E2EF"
RED = "#FF0000"

# Sizes
WIDTH = 800
HEIGHT = 600

# Path
PRJ_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


class Window:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("Graph Scheduler")
        self.window.geometry(f"{WIDTH}x{HEIGHT}")
        self.window.configure(background=LAVENDER)

        # Title and title box
        title_box = tk.Frame(self.window, bg=SILVER, width=WIDTH)
        title_box.pack(fill="x")
        title = tk.Label(title_box, text="Graph Scheduler", font=("Arial", 40), bg=SILVER, fg=BLACK)
        title.pack(pady=5)

        # File bar
        file_bar = tk.Frame(self.window, bg=OXFORD_BLUE, width=200, height=HEIGHT)
        file_bar.pack(fill="y", side="left")

        # File bar buttons
        file_bar_buttons = tk.Frame(file_bar, bg=OXFORD_BLUE, width=200, height=HEIGHT)
        file_bar_buttons.pack(fill="y", side="left")
        for file in os.listdir(PRJ_DIR + "/graphs"):
            if file.endswith(".txt"):
                file_bar_button = tk.Button(file_bar_buttons, text=file,background=RED, fg=SILVER, font=("Arial", 20), width=10, height=2)
                file_bar_button.pack(pady=5)

        self.window.mainloop()


Window()

【问题讨论】:

  • AFAIK 在 Mac 上没有按钮的背景颜色。

标签: python-3.x tkinter button background-color


【解决方案1】:

使用from tkmacosx import Button

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 2021-02-16
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2017-04-25
    • 2013-06-24
    相关资源
    最近更新 更多