2017-03-24 151 views
1

我正在研究一个具有垂直和水平滚动条的tkinter脚本。 垂直滚动条下方的一部分窗口未拾取我正在应用的背景颜色。Tkinter滚动条背景颜色问题

我已经尝试过下面的颜色选项,仍然有一小部分没有拿起。

图片:

采样窗口快照

全码:

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__': 

任何建议都欢迎。由于

-Vinay

+1

您是否有任何其他有黑色背景的容器(框架/画布等)?这部分可能不属于滚动条。 – Lafexlos

+0

不,我有两个框架,都具有相同的颜色。 – Vinay

+1

主窗口是否有黑色背景? –

回答

0

的一块黑色的是self,你guiList类的背景。添加一行self.config(bg="steel blue")到它的__init__()函数(或initUI()我想)来解决它。

+1

感谢您的回答。有效。 – Vinay