简体   繁体   English

将滚动条添加到tkinter框架

[英]Adding a Scrollbar to a tkinter frame

I have the following piece of code which lists some urls when you click a button. 我有以下一段代码,当您单击按钮时列出了一些URL。 I would like to add scrollbars to the bottom frame where the urls are listed. 我想将滚动条添加到列出网址的底部框架中。 Unfortunately,my current attempts leads to "cannot use geometry manager grid inside .!frame2 which already has slaves managed by pack". 不幸的是,我目前的尝试导致“无法在已经由包管理从站的。!frame2内部使用几何管理器网格”。

I can see that I am mixing grid and pack, but I have not managed so far to solve this. 我可以看到我正在混合网格和打包,但是到目前为止我还没有解决这个问题。 Could anyone help me, please? 有人可以帮我吗?

import random
import tkinter as tk
import webbrowser

class Pierre:
        """This class holds all the objects, data and functions for a single 
    line"""
    def __init__(self, master, url):
        self.url = url
        self.counter = 0
        _, i = master.grid_size() # get the current row number

        lbl = tk.Label(master, text=url, fg="blue", cursor="hand2")
        lbl.grid(row=i, column=1)
        lbl.bind("<Button-1>", self.callback)

        self.DisplayButton = tk.Button(master, text = self.counter)
        self.DisplayButton.grid(row=i, column=2)
        self.DisplayButton.config(height = 1, width = 1 )

        self.Plus1Button = tk.Button(master, text = "+1", command=self.plus1, bg="green")
        self.Plus1Button.grid(row=i, column=3)
        self.Plus1Button.config(height = 1, width = 1 )

        self.Neg1Button = tk.Button(master, text = "-1", command=self.neg1, bg="green")
        self.Neg1Button.grid(row=i, column=4)
        self.Neg1Button.config(height = 1, width = 1 )

    def plus1(self):
        self.counter += 1
        self.DisplayButton["text"]=str(self.counter)

    def neg1(self):
        self.counter -= 1
        self.DisplayButton["text"]=str(self.counter)

    def callback(self, event):
        webbrowser.open_new(self.url)

class TestClass(tk.Tk):
    def __init__(self, **kwargs):
        tk.Tk.__init__(self, **kwargs)
        self.title('Test')

        self.topframe = tk.Frame(self)
        self.topframe.pack( side = tk.TOP, pady=30)

        self.bottomframe = tk.Frame(self)
        self.bottomframe.pack( side = tk.BOTTOM )

        self.canvas=tk.Canvas(self.bottomframe)
        self.hbar=tk.Scrollbar(self.bottomframe,orient=tk.HORIZONTAL)
        self.hbar.pack(side=tk.BOTTOM,fill=tk.X)
        self.hbar.config(command=self.canvas.xview)
        self.vbar=tk.Scrollbar(self.bottomframe,orient=tk.VERTICAL)
        self.vbar.pack(side=tk.RIGHT,fill=tk.Y)
        self.vbar.config(command=self.canvas.yview)
        self.canvas.config(width=300,height=300)
        self.canvas.config(xscrollcommand=self.hbar.set, 
    yscrollcommand=self.vbar.set)
        self.canvas.pack(side=tk.LEFT,expand=True,fill=tk.BOTH)


        self.button = tk.Button(self.topframe, text='Click', command = 
    self.output_value)
        self.button.pack(side="left", fill="both", expand=True)

    ####define the function that the submit button will do
    def output_value(self):
        urls = ["http://www.google.com", "http://www.facebook.com"]
        for url in urls:
            Pierre(self.bottomframe, url)

if __name__ == "__main__":
    root = TestClass()
    root.mainloop()

As the error you get indicates, you should not use both pack and grid in the same parent. 正如您得到的错误所示,您不应在同一父级中同时使用packgrid You should read this post gives you more insight into what you are doing when you use both pack and grid , and also gives you an alternative: conceptual dividing your interface into segments. 您应该阅读这篇文章 ,可以更深入地了解同时使用packgrid ,并且还可以提供另一种选择:从概念上将您的界面划分为多个部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM