所以我正在创建一个Python Tkinter应用程序。目前我正在尝试将应用程序的背景设置为Python的默认灰色,但我遇到的问题是部分屏幕没有正确着色。
This is what the screen currently looks like
现在我去故意将当前帧的背景颜色为蓝色,并且容器的背景为红色,这样我就可以尝试看到无色的区域。正如你所看到的那样,白色的灰色区域既不是容器的一部分,也不是框架的一部分,我无法弄清楚如何让它变色。这是我的代码:
Main.py:
from Interface import Interface
#Development environent variable. Set to false when program is released to public
inDevelopment = True
#Sets up the Window
root = Interface(inDevelopment)
root['bg'] = 'red'
#Sets the Window TItle
root.title("Tester")
#Sets the Window size depending on if it is the release copy of the program
if inDevelopment:
#Sets the window size. Disabled when Fullscreen enabled
root.geometry("800x480")
if not inDevelopment:
#Sets the window to fullscreen. Disabled when screen dimensions enabled
root.attributes("-fullscreen", True)
root.mainloop()
Interface.py:
import tkinter as tk
from tkinter import ttk
from MeterGauge import Meter
TEXT_FONT = ("Arial", 11, "bold")
currentMeter = None
voltageMeter = None
class Interface(tk.Tk):
def __init__(self, inDevelopment, *args, **kwargs):
self.inDevelopment = inDevelopment
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.grid(column = 0, row = 0, sticky = "nwes")
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = { }
for F in (DefaultScreen, TestScreen):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, padx = 25, pady = 25, sticky = "nsew")
self.show_frame(DefaultScreen)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
# Screen which will be the default display
# Emulates release copy of program
class DefaultScreen(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.config(background='blue')
frame_style = ttk.Style()
frame_style.configure("My.TFrame", background = "grey")
#Frame for Current Monitor
currentMeterFrame = ttk.Frame(controller, style="My.TFrame")
currentMeterFrame.place(height = 165, width = 185, x = 545, y = 25)
#Frame for Voltage Monitor
voltageMeterFrame = ttk.Frame(controller, style="My.TFrame")
voltageMeterFrame.place(height = 165, width = 185, x = 545, y = 250)
tc = TemperatureCommands()
#Label for the Controller State
lblControllerState = ttk.Label(self, text = "Controller State",
background = "grey", font = TEXT_FONT).grid(
column = 1, row = 1, columnspan = 5)
#Label for the Temperature State
lblTempState = ttk.Label(self, text = "this is test text",
background = "grey", font = TEXT_FONT)
lblTempState.grid(column = 1, row = 2, columnspan = 5)
lblTempState.config(font=("", 14, "bold"))
ttk.Label(self, width=20, background = "grey").grid(column = 1, row = 3, columnspan = 2)
#High Cool Button
btnHighCool = ttk.Button(self, text="High Cool", command = tc.HighCool).grid(
column = 1, row = 4, columnspan = 2, rowspan = 2, sticky = "nsew")
#Medium Cool Button
btnMediumCool = ttk.Button(self, text="Medium Cool", command = tc.MediumCool).grid(
column = 1, row = 6, columnspan = 2, rowspan = 2, sticky = "nsew")
#Low Cool Button
btnLowCool = ttk.Button(self, text="Low Cool", command = tc.LowCool).grid(
column = 1, row = 8, columnspan = 2, rowspan = 2, sticky="nsew")
#OFF Button
btnOff = ttk.Button(self, text = "OFF", command = tc.Off).grid(
column = 1, row = 10, columnspan = 5, rowspan = 2, sticky = "nsew")
#Driver Button (disabled for release version)
if controller.inDevelopment:
btnDriver = ttk.Button(self, text= "Driver", command = tc.Driver).grid(
column = 1, row = 12, columnspan = 2, rowspan = 2, sticky = "nsew")
#Passenger Button (disabled for release version)
if controller.inDevelopment:
btnPassenger = ttk.Button(self, text = "Passenger", command = tc.Passenger).grid(
column = 4, row = 12, columnspan = 2, rowspan = 2, sticky = "nsew")
ttk.Label(self, width = 0, background = "grey").grid(column = 3, row = 3)
ttk.Label(self, width = 20, background = "grey").grid(column = 4, row = 3, columnspan = 2)
#High Heat Button
btnHighHeat = ttk.Button(self, text="High Heat", command = tc.HighHeat).grid(
column = 4, row = 4, columnspan = 2, rowspan = 2, sticky = "nsew")
#Medium Heat Button
btnMediumHeat = ttk.Button(self, text = "Medium Heat", command = tc.MediumHeat).grid(
column = 4, row = 6, columnspan = 2, rowspan = 2, sticky= "nsew")
#Low Heat Button
btnLowHeat = ttk.Button(self, text = "Low Heat", command = tc.LowHeat).grid(
column = 4, row = 8, columnspan = 2, rowspan = 2, sticky = "nsew")
fill_Width = 5
col_Num = 6
for i in range(14):
ttk.Label(self, width = fill_Width, background = "grey").grid(column = col_Num, row = i)
row_Num = 14
col_Num = 7
n = 0
col = 6
for n in range(col):
ttk.Label(self, width = fill_Width, background = "grey").grid(column = col_Num, row = row_Num)
col_Num = col_Num + 1
#Current Readout
ttk.Label(self, text = "Current Draw", background = "grey").grid(column = 7, row = 1)
lblCurrentDraw = ttk.Label(self, text = "0.00 A", foreground = "lime", background = "black").grid(
column = 7, row = 2, sticky = "ew")
#Voltage Readout
ttk.Label(self, text = "Voltage Tester", background = "grey").grid(column = 7, row = 8)
lblVoltageDraw = ttk.Label(self, text = "0.00 VDC", foreground = "lime", background = "black").grid(
column = 7, row = 9, sticky = "ew")
#Current Meter Gauge
currentMeter = Meter(currentMeterFrame, height = 300, width = 300, background = "grey", highlightthickness = 0)
currentMeter.SetRange(0.0, 5.0)
currentMeter.place(x = 10, y = 2)
current0 = ttk.Label(currentMeterFrame, text = "0.0", background = "grey").place(
height = 18, width = 28, x = 0, y = 141)
current1 = ttk.Label(currentMeterFrame, text = "1.0", background = "grey").place(
height = 18, width = 28, x = 4, y = 100)
current2 = ttk.Label(currentMeterFrame, text = "2.0", background = "grey").place(
height = 18, width = 28, x = 21, y = 60)
current3 = ttk.Label(currentMeterFrame, text = "3.0", background = "grey").place(
height = 18, width = 28, x = 54, y = 27)
current4 = ttk.Label(currentMeterFrame, text = "4.0", background = "grey").place(
height = 18, width = 28, x = 97, y = 7)
current5 = ttk.Label(currentMeterFrame, text = "5.0", background = "grey").place(
height = 18, width = 28, x = 144, y = 1)
#Voltage Meter Gauge
voltageMeter = Meter(voltageMeterFrame, height = 300, width = 300, background = "grey", highlightthickness = 0)
voltageMeter.SetRange(0, 15)
voltageMeter.place(x = 10, y = 2)
voltage1000 = ttk.Label(voltageMeterFrame, text = "0.0", background = "grey").place(
height = 18, width = 28, x = 0, y = 141)
voltage1200 = ttk.Label(voltageMeterFrame, text = "5.0", background = "grey").place(
height = 18, width = 28, x = 21, y = 60)
voltage1300 = ttk.Label(voltageMeterFrame, text = "10.0", background = "grey").place(
height = 18, width = 28, x = 54, y = 27)
voltage1500 = ttk.Label(voltageMeterFrame, text = "15.0", background = "grey").place(
height = 18, width = 28, x = 144, y = 1)
for child in self.winfo_children(): child.grid_configure(padx = 5, pady = 5)
# Screen for testing new items before adding them to the main display
class TestScreen(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
我不确切知道导致浅灰色区域出现的原因。任何帮助将不胜感激。