所以我从tkinter开始,有一个我无法解决的问题,当我使用网格定义我想要的东西在哪里时,网格中的每个单元格都不会具有所需的背景颜色。
比说明太多,显示起来更好:
import tkinter as tk
from tkinter import ttk
LARGE_FONT = ("arial", 20)
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
#self.grid_rowconfigure(0, weight=1)
#self.grid_columnconfigure(0, weight=1)
self.configure(background='black')
main_container = tk.Frame(self)
main_container.grid(column=0, row=0, sticky = "nsew")
main_container.grid_rowconfigure(0, weight = 1)
main_container.grid_columnconfigure(0, weight = 1)
main_container.configure(background="black")
menu_bar = tk.Menu(main_container)
file_menu = tk.Menu(menu_bar, tearoff = 0)
file_menu.add_command(label = "Save settings", command = lambda: popupmsg("Not supported yet!"))
file_menu.add_separator()
file_menu.add_command(label = "Exit", command = quit)
menu_bar.add_cascade(label = "File", menu = file_menu)
tk.Tk.config(self, menu = menu_bar)
self.frames = {}
for fr in (MainPage,):
frame = fr(main_container, self)
self.frames[fr] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(MainPage)
def show_frame(self, pointer):
frame = self.frames[pointer]
frame.tkraise()
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.columnconfigure(0, weight = 1)
self.columnconfigure(1, weight = 1)
self.rowconfigure(0, weight = 1)
self.rowconfigure(1, weight = 1)
label = tk.Label(self, text = "Main Page", fg = "white", bg="black", font = LARGE_FONT)
label.grid(row = 0, column = 0, padx = 10, pady = 10)
label2 = tk.Label(self, text = "Main Page2", fg = "white", bg="black", font = ("Arial",8))
label2.grid(row = 1, column = 0, padx = 10, pady = 10, sticky = 'ne')
button2 = ttk.Button(self, text = "Page 2", command = lambda: controller.show_frame(Page2))
button2.grid(row = 0, column = 1, sticky = 'nswe')
button3 = ttk.Button(self, text = "Exit", command = quit)
button3.grid(row = 1, column = 1, sticky = 'nswe')
app = Main()
app.geometry("1280x720")
app.mainloop()
如您所见,从这里开始: screen
重置网格内部的背景,并且在所有可能的地方添加了背景修饰符,但仍然无法使它起作用
PS:我知道我应该在开始时取消注释两行,以使其按我的意愿工作,但这只是为了表明框架背景正在工作,而网格一则由于某些原因而不同
预先感谢, Shraneid
答案 0 :(得分:2)
grid
不能更改任何颜色。 grid
不是“事物”,本身也不具有颜色或更改任何颜色。如果您希望MainPage
的背景为黑色,则需要将其设置为黑色。
如果要对其进行硬编码,只需在调用超类的__init__
方法时将其添加:
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, background="black")
答案 1 :(得分:1)
重置网格内的背景,并且在所有可能的地方添加了背景修饰符,但仍然无法正常工作
背景未重置。框架包含自己的背景颜色,因此您需要告诉您的MainPage
类(也称为框架),您希望它是哪种颜色。
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
# Add this line here
self.configure(background='black')