Python tkinter - 画布滚动条切断了框架内容

时间:2018-05-25 06:34:33

标签: python tkinter scrollbar tkinter-canvas

我正在为航海假期写一个简单的日志应用程序。因此,我想创建一个小的gui,用于在日志行中输入数据。 我正在使用带有框架和画布的网格布局作为主框架,以显示滚动条,当有多行时。 一切都很好看,直到命令

self.canvasCenter.configure(scrollregion=self.canvasCenter.bbox("all"))

被调用。如果我评论这一行,画布正在使用全帧。如果此行处于活动状态 - 画布仅使用可用屏幕的50%并且日期被截断。

我试图将代码删除到必要的东西(不使用任何其他文件,配置,类等)。所以它可以独立执行:

#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

import sys
if sys.version_info.major == 2:
    # We are using Python 2.x
    import Tkinter as tk
elif sys.version_info.major == 3:
    # We are using Python 3.x
    import tkinter as tk

#from lib import logBookData
#from config import *

#Titel of the application shown in title bar
appTitle = "My APP"

#Layout geometry (size of the window)
lWidth = 768
lHeight = 900
layoutGeometry = '900x768'

#Colours for foreground and background for labels and entry fields
#headerBG = "Light Blue"
headerBG = "white"
labelBG = "white"
labelFG = "black"

#Number of columns (keep in Sync with headers and width list below!)
cNumber = 14

#Column width for number of columns defined above 
cWidth = [5,7,5,5,5,5,5,5,5,5,5,5,5,10]
cHeaders = ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13", "C14" ]


class logBookGUI(tk.Tk):
    def __init__(self,parent):
        tk.Tk.__init__(self,parent)
        self.parent = parent
        self.lineCount = 0
        self.initialize()

    def initialize(self):
        #create a masterframe
        self.masterFrame = tk.Frame(self, bg=headerBG, bd=4, relief="groove") #, width = lWidth, height = lHeight)
        self.masterFrame.grid(sticky=tk.NSEW)
        self.masterFrame.columnconfigure(cNumber, weight=1)

        #create Headerframe for Standard Info
        self.headerFrame = tk.Frame(self.masterFrame, bg=headerBG, bd=2, relief="groove")
        self.headerFrame.grid(row=0, column=0, sticky=tk.NSEW)
        self.headerFrame.columnconfigure(cNumber, weight=1)
        #Label Bootsname
        boatNameLabel = tk.Label(self.headerFrame, text="Bootsname", anchor="w", fg=labelFG,bg=labelBG)
        boatNameLabel.grid(column=0, row=0, sticky=tk.NSEW)

        #Field Bootsname
        self.boatName = tk.StringVar()
        self.boatNameEntry = tk.Entry(self.headerFrame,textvariable=self.boatName, width = cWidth[0])
        self.boatNameEntry.grid(column=1,row=0, sticky=tk.NSEW)
        self.boatName.set(u"Solva")

        for i in xrange(cNumber-2):
            button = tk.Button(self.headerFrame,text=u"T %s" %i, command=self.OnButtonClick)
            button.grid(column=i,row=0, sticky=tk.NSEW)
        button = tk.Button(self.headerFrame,text=u"TEST", command=self.OnButtonClick)
        button.grid(column=cNumber-2,row=0, sticky=tk.NSEW)
        button = tk.Button(self.headerFrame,text=u"Neue Zeile", command=self.newLineClick)
        button.grid(column=cNumber-1,row=1, sticky=tk.NSEW)

        #create center frame
        self.centerFrame = tk.Frame(self.masterFrame, bg=headerBG, bd=2, relief="groove")
        self.centerFrame.grid(row=1, column=0, sticky=tk.NSEW)
        self.centerFrame.columnconfigure(cNumber, weight=1)
        #create a canvas for center frame
        self.canvasCenter = tk.Canvas(self.centerFrame)
        self.canvasCenter.grid(row=0, column=0, sticky=tk.NSEW)
        self.canvasCenter.columnconfigure(cNumber, weight=1)
        # Create a vertical scrollbar linked to the canvas.
        vsbar = tk.Scrollbar(self.centerFrame, orient=tk.VERTICAL, command=self.canvasCenter.yview)
        vsbar.grid(row=0, column=cNumber, sticky=tk.NSEW)
        self.canvasCenter.configure(yscrollcommand=vsbar.set)

        # Create a frame on the canvas to contain the content.
        self.contentFrame = tk.Frame(self.canvasCenter, bg="Red", bd=2, relief="groove")
        self.contentFrame.grid(row=0, column=0, sticky=tk.NSEW)
        self.contentFrame.columnconfigure(cNumber, weight=1)
        column = 0
        for header in cHeaders:
            label = tk.Label(self.contentFrame, text=header, anchor="w", fg=labelFG,bg=labelBG,borderwidth=2,relief="groove", width = cWidth[column])
            label.grid(column = column, row=1, sticky="EW")
            column += 1

        self.addLine()
        # Create canvas window to hold the centerframe.
        self.canvasCenter.create_window((0,0), window=self.contentFrame, anchor=tk.NW)
        # Update buttons frames idle tasks to let tkinter calculate buttons sizes
        self.contentFrame.update_idletasks()
        self.canvasCenter.configure(scrollregion=self.canvasCenter.bbox("all"))
        self.grid_columnconfigure(cNumber,weight=1)
        self.resizable(True,False)
        self.update()
        self.geometry("%sx%s" % (lHeight, lWidth))       

    def newLineClick(self):
        self.addLine()

    def OnButtonClick(self):
        pass
    def OnPressEnter(self,event):
        pass

    def addLine(self):
        tkLine = []
        try:
            for i in xrange(cNumber):
                self.entryVar = tk.StringVar()
                self.entry =  tk.Entry(self.contentFrame,textvariable=self.entryVar,width=cWidth[i])
                self.entry.grid(column=i, row = self.lineCount + 3, sticky='EW')
                tkLine.append(self.entryVar)
                self.canvasCenter.configure(scrollregion=self.canvasCenter.bbox("all"))
        except IndexError:
            print ("Error in config file. Number of columns and given columns do not match - check file config.py")
            sys.exit() 

        self.update()
        self.lineCount += 1

if __name__ == "__main__":
    app = logBookGUI(None)
    app.title(appTitle)
    app.mainloop()

有人可以给我一个如何使用scrollregion的建议,以便使用框架的整个尺寸吗?

非常感谢,    的Gernot

0 个答案:

没有答案