tkinter 中带有滚动条的动态按钮 - Python [英] Dynamic Button with ScrollBar in tkinter - Python

查看:42
本文介绍了tkinter 中带有滚动条的动态按钮 - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 tkinter 窗口中创建动态按钮,但我尝试了滚动条选项,它不能帮助我滚动 tkinter 窗口中的按钮,是否还有其他选项可以滚动动态按钮.

I had an requirement for creating dynamic buttons in tkinter window,But i tried Scroll bar option which is not helping me to scroll the buttons in the tkinter window,Is any other option to scroll the Dynamic buttons.

代码:

root = tkinter.Tk()
root.title("Links-Shortcut")
root.configure(background="gray99")
sw= tkinter.Scrollbar(root) 
sw.pack(side=RIGHT, fill=Y)


os.chdir("C:\Bo_Link")
with open('Bo_ol_links.csv', 'r', newline='') as fo:
    lis=[line.strip('\r\n').split(',') for line in fo]        # create a list of lists
lis=sorted(lis)
    #print (lis)

for i,x in enumerate(lis):
    btn = tkinter.Button(root,height=1, width=20,relief=tkinter.FLAT,bg="gray99",fg="purple3",font="Dosis",text=lis[i][0],command=lambda i=i,x=x: openlink(i))
    btn.pack(padx=10,pady=5,side=tkinter.TOP)

def openlink(i):
    os.startfile(lis[i][1])

root.mainloop()

谢谢.

推荐答案

此代码将按钮打包到一个可滚动的框架中,我在 Tkinter Unpythonic Wiki.我在 Python 2 上运行它,所以我使用 Tkinter 作为 import 语句中的模块名称,对于 Python 3,将该语句更改为使用 tkinter.

This code packs buttons into a scrollable Frame that I stole from found at the Tkinter Unpythonic Wiki. I'm running it on Python 2, so I use Tkinter as the module name in the import statement, for Python 3 change that statement to use tkinter.

import Tkinter as tk

class VerticalScrolledFrame(tk.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):
        tk.Frame.__init__(self, parent, *args, **kw)            

        # create a canvas object and a vertical scrollbar for scrolling it
        vscrollbar = tk.Scrollbar(self, orient=tk.VERTICAL)
        vscrollbar.pack(fill=tk.Y, side=tk.RIGHT, expand=tk.FALSE)
        canvas = tk.Canvas(self, bd=0, highlightthickness=0,
                        yscrollcommand=vscrollbar.set)
        canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=tk.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 = tk.Frame(canvas)
        interior_id = canvas.create_window(0, 0, window=interior,
                                           anchor=tk.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)


root = tk.Tk()
root.title("Scrollable Frame Demo")
root.configure(background="gray99")

scframe = VerticalScrolledFrame(root)
scframe.pack()

lis = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
for i, x in enumerate(lis):
    btn = tk.Button(scframe.interior, height=1, width=20, relief=tk.FLAT, 
        bg="gray99", fg="purple3",
        font="Dosis", text='Button ' + lis[i],
        command=lambda i=i,x=x: openlink(i))
    btn.pack(padx=10, pady=5, side=tk.TOP)

def openlink(i):
    print lis[i]

root.mainloop()

这篇关于tkinter 中带有滚动条的动态按钮 - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆