英文:
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'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't use tkmacosx because i need this project to be runnable on windows and linux as well.
Thank you for your help, don'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 = "#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()
答案1
得分: 0
use from tkmacosx import Button
英文:
use from tkmacosx import Button
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论