Tkinter网格中的滚动条

时间:2012-11-08 14:28:40

标签: python grid tkinter scrollbar

  

可能重复:
  Adding a scrollbar to a grid of widgets in Tkinter

在我的项目中,我希望在窗口中显示我的结果,使用Tkinter作为GUI。我将它们放在一种表中,使用网格小部件,窗口分为两个不同的部分(对于不同的结果)。但是长时间运行后,显示的结果数超过了我的屏幕高度,所以我想在我的程序中添加一个滚动条。我已经在stackoverflow上查看了几个问题,最接近的答案是:

Adding a scrollbar to a group of widgets in Tkinter (只是为了让你知道我在寻找什么!)

我无法将其应用到我的程序中,可能是因为我对Python很新,有时候我认为我是一个带有教程示例的Frankenstein博士。

我现在尝试了很多,但我不能让画面显示在画布上,可能只是我遗漏的一些小东西。

我创建了一个我的程序的抽象示例(没有滚动条),所以你知道我在做什么,也许有人可以帮助我获得它所属的滚动条!

非常感谢你!

示例代码:(运行)

import Tkinter as tk
toprow=1
botrow=1
class ProgramWindow(tk.Frame): 

    def __init__(self,name): 
        self.name = name
        tk.Frame.__init__(self,root)
        self.pack()

        if name=="BotWin":
            tk.Label(self,text="FirstColBot",width=30).grid(row=0,column=0)            
            tk.Label(self,text="SecndColBot",width=20).grid(row=0,column=1)

        elif name=="TopWin":
            tk.Label(self,text="FirstColTop",width=30).grid(row=0,column=0)         
            tk.Label(self,text="SecndColTop",width=20).grid(row=0,column=1)

    def addrowTop(self,stuff,otherstuff):
        global toprow

        textfield = tk.Text(self,width=30,height=1)
        textfield.grid(row=toprow,column=0)
        textfield.insert('0.0',stuff)

        textfield = tk.Text(self,width=20,height=1)
        textfield.grid(row=toprow,column=1)
        textfield.insert('0.0',otherstuff)

        toprow+=1

    def addrowBot(self,stuff,otherstuff):
        global botrow

        textfield = tk.Text(self,width=30,height=1)
        textfield.grid(row=botrow,column=0)
        textfield.insert('0.0',stuff)

        textfield = tk.Text(self,width=20,height=1)
        textfield.grid(row=botrow,column=1)
        textfield.insert('0.0',otherstuff)

        botrow+=1

def SomeProg():
    for i in range(20):
        if i%2==0:
            stuff = "Stuff is "+str(i)
            otherstuff=i*3
            Wins[0].addrowTop(stuff,otherstuff)
        elif i%2==1:
            stuff = "Stuff is "+str(i)
            otherstuff=i*4
            Wins[1].addrowBot(stuff,otherstuff)


root = tk.Tk()
root.title("Stuff")

Wins = [ ProgramWindow("TopWin"),ProgramWindow("BotWin")]
SomeProg()

root.mainloop()

我尝试添加滚动条的附加代码(基于上面链接中显示的示例)。 如果滚动条仅显示在下半部分,那就没关系,因为那部分结果很多。)

import Tkinter as tk
toprow=1
botrow=1
class ProgramWindow(tk.Frame): 

    def __init__(self,name): 
        self.name = name
        self.frame=tk.Frame.__init__(self,root)


        if name=="BotWin":
            tk.Label(self,text="FirstColBot",width=30).grid(row=0,column=0)            
            tk.Label(self,text="SecndColBot",width=20).grid(row=0,column=1)

            self.canvas = tk.Canvas(root, borderwidth=0, background="#ffffff")
            self.vsb = tk.Scrollbar(root, orient="vertical", command=self.canvas.yview)
            self.canvas.configure(yscrollcommand=self.vsb.set)

            self.vsb.pack(side="right", fill="y")
            self.canvas.pack(side="left", fill="both", expand=True)
            self.canvas.create_window((4,4), window=self.frame)

            self.bind("<Configure>", self.OnFrameConfigure)

        elif name=="TopWin":
            self.pack()
            tk.Label(self,text="FirstColTop",width=30).grid(row=0,column=0)         
            tk.Label(self,text="SecndColTop",width=20).grid(row=0,column=1)

    def addrowTop(self,stuff,otherstuff):
        global toprow

        textfield = tk.Text(self,width=30,height=1)
        textfield.grid(row=toprow,column=0)
        textfield.insert('0.0',stuff)

        textfield = tk.Text(self,width=20,height=1)
        textfield.grid(row=toprow,column=1)
        textfield.insert('0.0',otherstuff)

        toprow+=1

    def OnFrameConfigure(self, event):
        self.canvas.configure(scrollregion=self.frame.bbox("all"))

    def addrowBot(self,stuff,otherstuff):
        global botrow

        textfield = tk.Text(self,width=30,height=1)
        textfield.grid(row=botrow,column=0)
        textfield.insert('0.0',stuff)

        textfield = tk.Text(self,width=20,height=1)
        textfield.grid(row=botrow,column=1)
        textfield.insert('0.0',otherstuff)

        botrow+=1

def SomeProg():
    for i in range(20):
        if i%2==0:
            stuff = "Stuff is "+str(i)
            otherstuff=i*3
            Wins[0].addrowTop(stuff,otherstuff)
        elif i%2==1:
            stuff = "Stuff is "+str(i)
            otherstuff=i*4
            Wins[1].addrowBot(stuff,otherstuff)


root = tk.Tk()
root.title("Stuff")

Wins = [ ProgramWindow("TopWin"),ProgramWindow("BotWin")]
SomeProg()

root.mainloop()

2 个答案:

答案 0 :(得分:3)

Bryan Oakley's answer适应您的具体问题:

  • 使用画布作为父
  • 创建框架
  • 使用画布作为scrollregion
  • 的参数

注意:在python中进行子类化时,您不需要存储父__init__的结果,因为它在self上运行。

这是补丁:

     def __init__(self,name): 
         self.name = name
-        self.frame=tk.Frame.__init__(self,root)

         if name=="BotWin":
+            self.canvas = tk.Canvas(root, borderwidth=0, background="#ffffff")
+            tk.Frame.__init__(self,self.canvas)
             tk.Label(self,text="FirstColBot",width=30).grid(row=0,column=0)            
             tk.Label(self,text="SecndColBot",width=20).grid(row=0,column=1)
-            self.canvas = tk.Canvas(root, borderwidth=0, background="#ffffff")
             self.vsb = tk.Scrollbar(root, orient="vertical", command=self.canvas.yview)
             self.canvas.configure(yscrollcommand=self.vsb.set)

             self.vsb.pack(side="right", fill="y")
             self.canvas.pack(side="left", fill="both", expand=True)
-            self.canvas.create_window((4,4), window=self.frame)
+            self.canvas.create_window((4,4), window=self)
             self.bind("<Configure>", self.OnFrameConfigure)

         elif name=="TopWin":
+            self.frame=tk.Frame.__init__(self,root)
             self.pack()
             tk.Label(self,text="FirstColTop",width=30).grid(row=0,column=0)         
             tk.Label(self,text="SecndColTop",width=20).grid(row=0,column=1)
@@ -41,7 +40,7 @@
         toprow+=1

     def OnFrameConfigure(self, event):
-        self.canvas.configure(scrollregion=self.frame.bbox("all"))
+        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

     def addrowBot(self,stuff,otherstuff):
         global botrow

答案 1 :(得分:2)

从你的问题我很确定我可以帮助你,几个月前我遇到了类似的问题,我尝试了很多不同的解决方案,最后我从ttk模块中发现了ttk treeview小部件,标配python 2.7

这是一个帮助您了解我的意思的小例子

from Tkinter import *
import ttk
root = Tk()
treedata = [('column 1', 'column 2'), ('column 1', 'column 2')]
column_names = ("heading1", "heading2")
tree = ttk.Treeview(root, columns = column_names, yscrollcommand = scrollbar.set)
scrollbar = ttk.Scrollbar(root)
scrollbar.pack(side = 'right', fill= Y)
for x in treedata:
    tree.insert('', 'end', values =x)
for col in column_names: 
    tree.heading(col, text = col.Title())
scrollbar.config(command=tree.yview)
tree.pack()

希望这有助于这方面没有很多文档,但谷歌会帮助, 一个非常有用的链接:http://www.tkdocs.com/tutorial/tree.html

祝你好运:)