鱼C论坛

 找回密码
 立即注册
查看: 1849|回复: 9

一个关于tkinter的滚动条问题

[复制链接]
发表于 2017-12-20 18:02:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
linshi.png
大概框架是这样的:
左边整体是一个Frame,并且以其为master创建了好多的小的LabelFrame,数目超出了页面可以显示的范围(不止十个)
右半边是一个滚动条

问题是能否通过滚动条或者鼠标滚轮让在显示范围外的LabelFrame们移动到显示范围内?
在不改变窗口尺寸的情况下……
求大佬们帮忙
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-12-20 18:05:28 | 显示全部楼层
咦我修改头像还没有动静还是小乌龟
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-20 18:07:44 | 显示全部楼层
Zageku 发表于 2017-12-20 18:05
咦我修改头像还没有动静还是小乌龟

有动静了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-20 18:13:38 | 显示全部楼层
贴一下代码啊老哥
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-20 18:14:33 From FishC Mobile | 显示全部楼层
在组件中加入滚动模块试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-20 22:46:19 From FishC Mobile | 显示全部楼层
°蓝鲤歌蓝 发表于 2017-12-20 18:13
贴一下代码啊老哥

代码没什么特殊的啦,我只是希望可以实现frame和scroller的绑定
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-20 22:46:43 From FishC Mobile | 显示全部楼层
BngThea 发表于 2017-12-20 18:14
在组件中加入滚动模块试试

加上了不过不知道怎么和Frame绑定
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-20 22:56:35 | 显示全部楼层
Zageku 发表于 2017-12-20 22:46
代码没什么特殊的啦,我只是希望可以实现frame和scroller的绑定

好像scrollbar和Frame不能组合的样子。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-21 01:05:06 | 显示全部楼层
°蓝鲤歌蓝 发表于 2017-12-20 22:56
好像scrollbar和Frame不能组合的样子。

这个labelframe比较好看,想用这个放界面看来是不行了。。。labelframe能放到text里吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-21 23:10:45 | 显示全部楼层
找到了一个可以用的带滚动条Frame类:

class VerticalScrolledFrame(Frame):
    """A pure Tkinter scrollable frame that actually works!
    * Use the 'interior' attribute to place widgets inside the scrollable frame
    * Construct and pack/place/grid normally
    * This frame only allows vertical scrolling

    """
    def __init__(self, parent, *args, **kw):
        Frame.__init__(self, parent, *args, **kw)            

        # create a canvas object and a vertical scrollbar for scrolling it
        vscrollbar = Scrollbar(self, orient=VERTICAL)
        vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE)
        canvas = Canvas(self, bd=0, highlightthickness=0,
                        yscrollcommand=vscrollbar.set)
        canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
        vscrollbar.config(command=canvas.yview)

        # reset the view
        canvas.xview_moveto(0)
        canvas.yview_moveto(0)

        # create a frame inside the canvas which will be scrolled with it
        self.interior = interior = Frame(canvas)
        interior_id = canvas.create_window(0, 0, window=interior,
                                           anchor=NW)

        # track changes to the canvas and frame width and sync them,
        # also updating the scrollbar
        def _configure_interior(event):
            # update the scrollbars to match the size of the inner frame
            size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
            canvas.config(scrollregion="0 0 %s %s" % size)
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the canvas's width to fit the inner frame
                canvas.config(width=interior.winfo_reqwidth())
        interior.bind('<Configure>', _configure_interior)

        def _configure_canvas(event):
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the inner frame's width to fill the canvas
                canvas.itemconfigure(interior_id, width=canvas.winfo_width())
        canvas.bind('<Configure>', _configure_canvas)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-26 16:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表