Barra de rolagem na grade do Tkinter [duplicata]
Duplicata Possível:
Adicionando uma barra de rolagem a uma grade de widgets no Tkinter
No meu projeto, quero exibir meus resultados em uma janela, usando o Tkinter como GUI. Eu os coloco em uma espécie de tabela, usando o widget de grade, e a janela é separada em duas partes diferentes (para resultados diferentes). Mas depois de execuções mais longas, o número de resultados exibidos ultrapassa a altura da minha tela, então eu quero adicionar uma barra de rolagem ao meu programa. Eu já examinei várias questões aqui no stackoverflow, e a resposta que chegou mais perto foi esta:
Adicionando uma barra de rolagem a um grupo de widgets no Tkinter (apenas para que você saiba o que estou procurando!)
Eu não sou capaz de aplicar isso ao meu programa, talvez, porque eu sou bastante novo para o Python e às vezes acho que sou um Dr. Frankenstein com exemplos-tutorial.
Eu tentei muito agora, mas eu não consigo fazer com que as tabelas sejam exibidas na tela, possivelmente apenas uma pequena coisa que estou perdendo.
Eu criei um exemplo abstrato do meu programa (sem barra de rolagem) para que você saiba com o que estou trabalhando, talvez alguém de Você possa ajudar a obter essa barra de rolagem onde ela pertence!
Muito obrigado!
exemplo de código: (corre)
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()
código adicional com o meu tenta adicionar a barra de rolagem (com base no exemplo mostrado no link acima). se a barra de rolagem é mostrada apenas na parte inferior, tudo bem, já que é a parte com muitos resultados.)
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()