Background Button Color is not working properly MACOS

huangapple go评论61阅读模式
英文:

Background Button Color is not working properly MACOS

问题

我正在尝试为学校项目制作一个简单的GUI。我对使用tkinter还很陌生。我过去通常使用pygame来创建我的GUI,它非常适合自定义,但效率不太高哈哈。

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

我尝试使背景颜色变为红色。

我正在使用MACOS,我知道tkinter和mac之间存在一些复杂性,但我不能使用tkmacosx,因为我需要这个项目能够在Windows和Linux上运行。

谢谢你的帮助,如果你有关于tkinter常见实践的建议,我没有应用,或者你有解决我的问题的方法,请不要犹豫!

以下是输出和代码:

import tkinter as tk
import os

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

# 尺寸
WIDTH = 800
HEIGHT = 600

# 路径
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_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 = tk.Frame(self.window, bg=OXFORD_BLUE, width=200, height=HEIGHT)
        file_bar.pack(fill="y", side="left")

        # 文件栏按钮
        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()

输出图片


<details>
<summary>英文:</summary>

I am trying to make a simple GUI for a school project. I am new to using tkinter. I used to create my GUIs using pygame, which is great for custumization, but not really for efficiency haha.

I am creating a button for each file in my /graphs folder. In this code, line 42, it seems to me I can&#39;t change the background color of the button. This is a reccurent problemI have with tkinter. I have no idea if it is something I am doing wrong or if there is a problem with the framework I am using. 

I am trying to make the bg color red.

I am using MACOS, and i know there are complications with tkinter and mac, but i can&#39;t use tkmacosx because i need this project to be runnable on windows and linux as well.

Thank you for your help, don&#39;t hesitate if you have any suggestions on common practices with tkinter that i am not applying or if you have the solution to my problem !

Here is the output and the code
[Output][1]

```python
import tkinter as tk
import os

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

# 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(&quot;Graph Scheduler&quot;)
        self.window.geometry(f&quot;{WIDTH}x{HEIGHT}&quot;)
        self.window.configure(background=LAVENDER)

        # Title and title box
        title_box = tk.Frame(self.window, bg=SILVER, width=WIDTH)
        title_box.pack(fill=&quot;x&quot;)
        title = tk.Label(title_box, text=&quot;Graph Scheduler&quot;, font=(&quot;Arial&quot;, 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=&quot;y&quot;, side=&quot;left&quot;)

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

        self.window.mainloop()


Window()

答案1

得分: 0

use from tkmacosx import Button

英文:

use from tkmacosx import Button

huangapple
  • 本文由 发表于 2023年2月8日 18:34:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定