python - tkinter 中带有滚动条的动态按钮 - Python

标签 python python-3.x tkinter

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

代码:

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.

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()

关于python - tkinter 中带有滚动条的动态按钮 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31762698/

相关文章:

Python3 在网络摄像头 fps 处处理和显示网络摄像头流

python - 在 Python 中,不同的引号在这种情况下意味着什么?

python - 如何在 python 中使用正则表达式选择字符串之间的值并放置在数据框的列中

Python Setter 和 Getter 命名

python - Tkinter 获取图像以跟随光标并进行先前的移动

python - 尝试使用 Tkinter 将下拉列表放在代码上

python - Systemd + 非根 Gunicorn 服务 = 不存在的子进程

python - 导入错误 : No module named numpy

python Pandas : How do I create a column given a condition based on another column?

Python; urllib 错误 : AttributeError: 'bytes' object has no attribute 'read'