【问题标题】:Adding a Scrollbar to a tkinter frame将滚动条添加到 tkinter 框架
【发布时间】:2018-10-22 12:39:09
【问题描述】:

我有以下一段代码,当您单击按钮时列出了一些网址。我想在列出 url 的底部框架中添加滚动条。不幸的是,我目前的尝试导致“无法在 .!frame2 内使用几何管理器网格,它已经有由包管理的从属服务器”。

我可以看到我正在混合网格和包,但到目前为止我还没有设法解决这个问题。有人可以帮帮我吗?

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

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    正如您收到的错误所示,您不应在同一父级中同时使用 packgrid。 您应该阅读this post,让您更深入地了解同时使用packgrid 时所做的事情,并为您提供另一种选择:概念性地将您的界面划分为多个部分。

    【讨论】:

      猜你喜欢
      • 2013-01-24
      • 1970-01-01
      • 2021-12-24
      • 2022-10-06
      • 2013-04-17
      • 1970-01-01
      • 2019-12-26
      • 2020-02-09
      相关资源
      最近更新 更多