Skip to main content
 首页 » 编程设计

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

2025年05月04日35yyy_WW

我有在 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()