python中的画布控制_python-在Tkinter中动态更改画布的滚动区域

因此,我有一个自定义控件,它从frame继承并包含一个画布和一个滚动条,还有一个自定义控件,它也从我要动态添加到画布的框架继承,并根据需要调整滚动区域的大小.这是我的代码:

class MessageItem(Frame):

"""A message to be contained inside a scrollableContainer"""

def __init__(self, message, **kwds):

Frame.__init__(self, **kwds)

self.text = Label(self, text = message)

self.text.grid(column = 0, row = 0, sticky = N+S+E+W)

class scrollableContainer(Frame):

"""A scrollable container that can contain a number of messages"""

def initContents(self):

"""Initializes a scrollbar, and a canvas that will contain all the items"""

#the canvas that will contain all our items

self.canv = Canvas(self)

self.canv.grid(column = 0, row = 0, sticky = N+S+W)

#force Tkinter to draw the canvas

self.canv.update_idletasks()

#use the values from the canvas being drawn to determine the size of the scroll region

#note that currently, since the canvas contains nothing, the scroll region will be the same as

#the size of the canvas

geometry = self.canv.winfo_geometry()

xsize, ysize, xpos, ypos = parse_geometry_string(geometry)

self.canv['scrollregion'] = (0, 0, xsize, ysize)

#the scrollbar for that canvas

self.vscroll = Scrollbar(self, orient = VERTICAL, command = self.canv.yview )

self.vscroll.grid(column = 1, row = 0, sticky = N+S+E)

self.canv["yscrollcommand"] = self.vscroll.set

def __init__(self, **kwds):

Frame.__init__(self, **kwds)

#initialize the widget's contents

self.grid(sticky = N+S+E+W)

self.pack()

self.initContents()

#initialize the list of contents so we can append to it

self.contents = []

def addMessage(self, message):

#Add the message to the list of contents

self.contents.append(MessageItem(message))

#Add the message to the grid

self.contents[(len(self.contents) - 1)].grid(column = 0, row = (len(self.contents) - 1))

#set the new scrollable region for the canvas

scrollregionlist = self.canv['scrollregion'].split()

oldRegion = int(scrollregionlist[3])

newRegion = oldRegion + parse_geometry_string(self.contents[

(len(self.contents) - 1)].winfo_geometry())[3]

self.canv['scrollregion'] = (int(scrollregionlist[0]), int(scrollregionlist[1]),

int(scrollregionlist[2]), newRegion)

我遇到的问题是self.canv [‘scrollregion’]似乎在init之外消失了.在addMessage方法的这一行:

scrollregionlist = self.canv['scrollregion'].split()

self.canv上的scrollregion属性返回一个空字符串,我可以通过将其放在

print self.canv['scrollregion']

在那条线之前

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值