下面的代码最初是为了了解如何在框架中呈现URL列表并通过单击来访问它们而开发的。我也有几个按钮可以给他们打分。在我的项目中,我现在有很多网址,并且需要滚动条才能访问它们。我已经添加了一个,但是在可使用scollbar的框架部分未呈现url。实际上,我希望将整个底部框架链接到滚动条并用于呈现URL列表。我不明白我做错了什么。有人可以帮我吗?
编辑:按照下面的答案,下面的代码现在可以正常工作
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=0)
lbl.bind("<Button-1>", self.callback)
self.DisplayButton = tk.Button(master, text = self.counter)
self.DisplayButton.grid(row=i, column=1)
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=2)
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=3)
self.Neg1Button.config(height = 1, width = 1 )
master.update_idletasks()
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, width=250, height=190, bg="#EBEBEB")
self.bottomframe.pack( side = tk.BOTTOM )
self.canvas = tk.Canvas(self.bottomframe, width=250, height=190,
scrollregion=(0, 0, 1200, 800))
self.canvas.grid()
self.vscrollbar = tk.Scrollbar(self.bottomframe, orient=tk.VERTICAL,
command=self.canvas.yview)
self.vscrollbar.grid(row=0, column=5, sticky=tk.N+tk.S)
self.canvas['yscrollcommand'] = self.vscrollbar.set
self.frameCanvas=tk.Frame(self.canvas)
self.canvas.create_window((0,0),window=self.frameCanvas,anchor='nw')
#self.canvas.configure(scrollregion=self.canvas.bbox("all"))
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","http://www.google.com", "http://www.facebook.com", "http://www.google.com", "http://www.facebook.com"]
for url in urls:
Pierre(self.frameCanvas, url)
if __name__ == "__main__":
root = TestClass()
root.mainloop()
答案 0 :(得分:1)
框架不具有滚动功能。通常的方法是使用画布,然后使用create_window()
将小部件放置在画布上。