Python TKinter:当调整窗口大小时,框架和画布将水平扩展,但不会垂直扩展

时间:2014-02-13 22:26:46

标签: python canvas tkinter frame

出于某种原因,我可以让这个TKinter框架(allValOuterFrame)在调整窗口大小时垂直和水平扩展,但是,看起来框架所拥有的画布,以及该画布内的框架和垂直滚动条只会水平扩展。有人可以解释一下原因吗?

这是我的代码:

# At first I only had "from X import *" but then discovered that
# it is bad practice, so I also included "import X" statements so
# that if I need to use something from tk or ttk explicitly, I can,
# but I had 2/3 of my code done at that point so I had to leave in the
# "import *" things.
try:
    from Tkinter import * #python 2
    import Tkinter as tk
    from ttk import *
    import ttk
    import tkMessageBox as msg
    import tkFileDialog as openfile
except ImportError:
    from tkinter import * #python 3
    import tkinter as tk
    from tkinter.ttk import *
    import tkinter.ttk as ttk
    from tkinter import messagebox as msg
    from tkinter import filedialog as openfile
import csv
# My stuff:
from extractor import Analysis
from extractor import createDictionariesAndLists as getRawData
from nitrogenCorrector import correct as nCorrect
from carbonCorrector import correct as cCorrect


( ... )


def createAllValWindow(self):

    allValWindow = Toplevel(self)
    allValWindow.grab_set()

    if self.element == "N":
        allValWindow.title("All Nitrogen Raw Data Values")
    elif self.element == "C":
        allValWindow.title("All Carbon Raw Data Values")
    else:
        allValWindow.title("All Raw Data Values")

    allValOuterFrame = tk.Frame(allValWindow,background="#00FF00")
    allValCanvas = Canvas(allValOuterFrame, borderwidth=0)
    allValInnerFrame = Frame(allValCanvas, borderwidth=5)

    def allValOnFrameConfigure(event):
        allValCanvas.configure(scrollregion=allValCanvas.bbox("all"))

    allValOuterFrame.grid_rowconfigure(0,weight=1)
    allValOuterFrame.grid_columnconfigure(0,weight=1)
    allValInnerFrame.grid_rowconfigure(0,weight=1)
    allValInnerFrame.grid_columnconfigure(0,weight=1)

    allValVertScrollbar = Scrollbar(allValOuterFrame, orient="vertical",command=allValCanvas.yview)
    allValHorizScrollbar = Scrollbar(allValOuterFrame, orient="horizontal",command=allValCanvas.xview)
    allValCanvas.configure(yscrollcommand=allValVertScrollbar.set, xscrollcommand=allValHorizScrollbar.set)
    allValVertScrollbar.grid(row=1,column=12,sticky=N+S)
    allValHorizScrollbar.grid(row=2,column=0,columnspan=12,sticky=E+W)

    allValCanvas.grid(row=1,column=0,columnspan=12,sticky=N+S+E+W)        allValCanvas.create_window((4,4),window=allValInnerFrame,anchor="nw",tags="allValInnerFrame")
    allValInnerFrame.bind("<Configure>",allValOnFrameConfigure)
    allValDoneButton = Button(allValWindow,text="Done",command=allValWindow.destroy)
    allValOuterFrame.pack(fill="both",expand=1)
    allValDoneButton.pack()

    textRows = [self.rawHeader]
    textLabels = [[]]
    numDiffAminos = len(self.analyses) - 1 # Ignore the "trash" list at the end
    for singleAminoAnalyses in self.analyses[0:numDiffAminos]: # Once again, ignoring the last list
        if len(singleAminoAnalyses) < 1:
            continue
        for analysis in singleAminoAnalyses:
            textRows.append([str(analysis.analysisID),
                             str(analysis.row),
                             str(analysis.identifier1),
                             str(analysis.identifier2),
                             str(analysis.comment),
                             str(analysis.peakNumber),
                             str(analysis.rt),
                             str(analysis.component),
                             str(analysis.areaAll),
                             str(analysis.ampl), 
                             str(analysis.r), 
                             str(analysis.delta)])
            textLabels.append([])
    for i in range(len(textRows)):
        if i == 0:
            listRow = i
        else:
            listRow = i+1
        for j in range(len(textRows[i])):
            if i == 0:
                textLabels[i].append(Label(allValInnerFrame,text=textRows[i][j],font=("Fixedsys",10,"bold")))
            else:
                textLabels[i].append(Label(allValInnerFrame,text=textRows[i][j]))
            if j == 9:
                textLabels[i][j].grid(row=listRow,column=j,sticky=W+E,padx=(4,10))
            else:
                textLabels[i][j].grid(row=listRow,column=j,sticky=W+E,padx=(4,4))
        if i == 0:
            separator = tk.Frame(allValInnerFrame, height=2, borderwidth=1, bg="black", relief=SUNKEN)
            separator.grid(row=1,column=0,columnspan=12,sticky=W+E)

1 个答案:

答案 0 :(得分:0)

这是因为你给AllValOuterFrame的第0行权重为1,但你将画布放在第1行。如果你将画布移动到第0行或给第1行增加1的权重,它会调整大小你期望的方式。