Arthes
Arthes

Reputation: 79

Tkinter background color issue at scrollbar

I am working on a tkinter script which has a vertical and horizontal scrollbar. A portion of the window below the vertical scrollbar is not picking up the background color I'm applying.

I have tried the following color options still the small portion is not picking up.

Image:

Sample window snapshot

Full Code:

from tkinter.tix import *
from tkinter import *
import collections

root = Tk()
root.configure(background='steel blue')
# Global variables
fname = ''

# Variables for setting the height and width of widget
# Variables to set Height
actualRootHeight = 300
rootHScale = 25
rootHOffset = 100
canvasHeight = 300
root2CanvasHMargin =65 

# Variables to set Width
rootWScale = 10
rootWOffset = 200
canvasWidth  = 300
root2CanvasWMargin = 20
inpWidth = 0

# Lists to save configs
inpParamList = collections.OrderedDict()
paramListRef = collections.OrderedDict()
updatedParamList = collections.OrderedDict()
entryList = []
labels = []

# All widget coding is done here
class guiList(Frame):
    global root
    # Constructor - Use as a control structure
    def __init__(self,parent):
        Frame.__init__(self,parent)
        self.parent = parent
        self.readParams()
        self.setGeometry()
        self.initUI()
        self.controlUI()

    def onFrameConfigure(self, event, Canvas1):
        # Reset the scroll region to encompass the inner frame
        Canvas1.configure(scrollregion=Canvas1.bbox("all"), background='steel blue')

    # All widget edition is done here
    def initUI(self):
        global paramListRef
        titleStr = sys.argv[1]
        self.parent.title(titleStr)
        self.grid(row=0, column=0)
        inpConfigs = inpParamList.items()
        # Add a canvas and call Frame as it's child widget
        # Scrollbar can be added to Canvas
        Canvas1 = Canvas(self, width=canvasWidth, height=canvasHeight, borderwidth=0, bg="light steel blue")
        vsb = Scrollbar(self, orient="vertical", command=Canvas1.yview, bg="light steel blue", troughcolor="steel blue", highlightcolor="light steel blue", activebackground="light steel blue", highlightbackground="light steel blue")
        Canvas1.configure(yscrollcommand=vsb.set)
        vsb.grid(column=2, sticky='NS')
        hsb = Scrollbar(self, orient="horizontal", command=Canvas1.xview, bg="light steel blue", troughcolor="steel blue")
        Canvas1.configure(xscrollcommand=hsb.set)
        hsb.grid(column=0, sticky='EW')
        Canvas1.grid(row=0, column=0, sticky='NWES') 
        # Create new Frame for input configs
        Frame1 = Frame(Canvas1, width=canvasWidth, height=canvasHeight, bg="light steel blue")
        Canvas1.create_window((1,1),window=Frame1, anchor="nw", tags="Frame1")
        Frame1.bind("<Configure>", lambda event, arg=Canvas1: self.onFrameConfigure(event, arg))

        # Loop through the input configs
        i = 0
        # Add label and combobox in loop
        for k,v in inpConfigs:
            # Label widgets
            lbl1 = Label(Frame1, text=k, bg="light steel blue", font=("Helvetica", 12, "bold"), fg="steel blue")
            lbl1.grid(row = i, column = 0, padx=10, pady=5, sticky='W')   
            labels.append(lbl1)

            # Combo-box widget for configurations
            tkvar = StringVar(Frame1)
            tkvar.set(v[0])
            entry1 = OptionMenu(Frame1, tkvar, *v)
            entry1.configure(width=20, anchor=W,  bg="steel blue", fg="white", font=("Helvetica", 11, "bold"))
            entry1.grid(row = i, column=1, padx=10, pady=5, sticky='E')
            #entry1.grid_columnconfigure(2, weight=2)
            paramListRef[k] = tkvar
            i += 1

    # Read the updated configs after the button click
    def readUpdatedParams(self):
        global updatedParamList
        for k,v in paramListRef.items():
            updatedParamList[k] = v.get()
        root.destroy()
        self.writeBack()

    # Seperate Frame for buttons
    # Upon clicking read updted params
    def controlUI(self):
        Frame2 = Frame(self, bg="steel blue")
        Frame2.grid(row=2, column = 0, sticky="EW")
        b = Button(Frame2, text="OK", command=self.readUpdatedParams, bg="light steel blue", fg="steel blue", font=("Helvetica", 11, "bold"))
        #b.grid(row=1, column=1, pady=10)
        b.pack(fill="none", expand=True, pady = 10)

    # Read the file and create a key, value pair for configs
    # Lines in file is split with space as delimiter
    # First column is the config name and rest are all possible value
    def readParams(self):
        global inpParamList
        global inpWidth
        f = open(fname)
        for line in f:
            val = {}
            val = line.split()
            key = val.pop(0)
            # Get the max width of key to adjust widget width
            inpWidth = len(key) if (len(key) > inpWidth) else inpWidth
            inpParamList[key] = val

    # Geometry ( X-width x Y-width + X-position + Y-position)
    # Based on the number of elements in the config list
    # the height of the widget is set (Max is 75% of screen size)
    def setGeometry(self):
        global actualRootHeight
        global canvasWidth
        global canvasHeight
        listLen = len(inpParamList)
        rootWinwidth = int(inpWidth *rootWScale) + rootWOffset
        rootWinheight = (listLen * rootHScale ) + rootHOffset
        screenWidth = self.winfo_screenwidth()
        screenHeight = int(self.winfo_screenheight() * 0.75)
        if rootWinheight < screenHeight :
            actualRootHeight = rootWinheight
        else :
            actualRootHeight = screenHeight
        canvasWidth  = rootWinwidth - root2CanvasWMargin
        canvasHeight = actualRootHeight - root2CanvasHMargin
        rootWinresolution = str(rootWinwidth)+'x'+str(actualRootHeight)+'+'+'0'+'+'+'0'
        root.geometry(rootWinresolution)

    # Sub routine to write back the config file.
    def writeBack(self):
        fr = open("bt_top.param.config",'w')           
        for k,v in updatedParamList.items():
            print(k,v)
            fr.write(k)
            fr.write(" ")
            fr.write(v)
            fr.write("\n")
        fr.close()

# Main Function
# Define Window geometry and other top level stuff here
# Do not go into widget coding here
def main():
    global fname
    # Get File name from command line argument        
    fname = sys.argv[2]
    app = guiList(root)
    root.mainloop()

# The __name__ variable decides what to run
# Below lines make this file run stand-alone
if __name__ == '__main__':

Any suggestions are welcome. Thanks

-Vinay

Upvotes: 1

Views: 3087

Answers (1)

Junuxx
Junuxx

Reputation: 14261

The piece of black is the background of self, your guiList class. Add a line self.config(bg="steel blue") to its __init__() function (or to initUI() I suppose) to fix it.

Upvotes: 1

Related Questions