我希望我的文本小部件在每个文本小部件内都具有scrollbar
功能。我创建了一个canvas
,并在其中使用frame
嵌入了一个create_window
。然后,我将2个文本小部件放入此框架中。我希望每个文本小部件都具有一个滚动条,但是当我添加滚动条时,它们之一适合整个框架,而第二个适合整个框架的一半。我该如何解决这个问题?
import tkinter as tk
import os
import tkinter.filedialog
import tkinter.messagebox
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
'''This initialisation runs the whole program'''
#textBoxList = []
tk.Tk.__init__(self, *args, **kwargs)
self.title('Untitled')
self.geometry('500x500')
self.canvas = tk.Canvas(self)
self.scroll = tk.Scrollbar(self, orient='vertical', command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.scroll.set)
self.frame = tk.Frame(self.canvas) # frame does not get pack() as it needs to be embedded into canvas throught canvas.
self.scroll.pack(side='right', fill='y')
self.canvas.pack(fill='both', expand='yes')
self.canvas.create_window((0,0), window=self.frame, anchor='nw')
self.frame.bind('<Configure>', self.canvas.configure(scrollregion=self.canvas.bbox('all')))
# 1st Text Widget
self.journal = tk.Text(self.frame)
self.vsb = tk.Scrollbar(self.frame)
self.vsb.config(command=self.journal.yview)
self.journal.config(yscrollcommand=self.vsb.set)
self.vsb.pack(side='right', fill='y')
self.journal.pack()
#2nd Text Widget
self.good = tk.Text(self.frame)
self.vsb2 = tk.Scrollbar(self.frame)
self.vsb2.config(command=self.good.yview)
self.good.config(yscrollcommand=self.vsb2.set)
self.vsb2.pack(side='right', fill='y')
self.good.pack()
root = Main()
root.mainloop()
最佳答案
两个问题(除了粘贴代码时可能发生的混乱缩进):frame
绑定到函数的返回值,而不是函数本身。您可以使用lambda
函数解决此问题。
您正在尝试使用pack()
创建复杂的布局。请尝试使用grid()
。
import tkinter as tk
import os
import tkinter.filedialog
import tkinter.messagebox
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
'''This initialisation runs the whole program'''
#textBoxList = []
tk.Tk.__init__(self, *args, **kwargs)
self.title('Untitled')
self.geometry('500x500')
self.canvas = tk.Canvas(self)
self.scroll = tk.Scrollbar(self, orient='vertical', command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.scroll.set)
self.frame = tk.Frame(self.canvas) # frame does not get pack() as it needs to be embedded into canvas throught canvas.
self.scroll.pack(side='right', fill='y')
self.canvas.pack(fill='both', expand='yes')
self.canvas.create_window((0,0), window=self.frame, anchor='nw')
self.frame.bind('<Configure>', lambda x: self.canvas.configure(scrollregion=self.canvas.bbox('all'))) # lambda function
# 1st Text Widget
self.journal = tk.Text(self.frame)
self.vsb = tk.Scrollbar(self.frame)
self.vsb.config(command=self.journal.yview)
self.journal.config(yscrollcommand=self.vsb.set)
self.journal.grid(row=0, column=0) # grid instead
self.vsb.grid(row=0, column=1, sticky='ns') # grid instead
#2nd Text Widget
self.good = tk.Text(self.frame)
self.vsb2 = tk.Scrollbar(self.frame)
self.vsb2.config(command=self.good.yview)
self.good.config(yscrollcommand=self.vsb2.set)
self.good.grid(row=1, column=0) # grid instead
self.vsb2.grid(row=1, column=1, sticky='ns') # grid instead
root = Main()
root.mainloop()
关于python - 如何在tkinter上的框架中具有多个带有滚动条的文本小部件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30897813/