我的滚动条有点问题。功能上一切都很好。出现滚动条,对插入文本小部件和滚动工作做出反应。但是在启动应用程序之后,所有滚动条的箭头按钮都会变灰,直到我调整应用程序窗口的大小。网格设置中的框架有什么问题吗?我分为四个部分,左上(UL),右上(UR),左下(LL),右下(LR)。
import Tkinter
from Tkinter import *
class findhash_tk(Tkinter.Tk):
def __init__(self, parent):
Tkinter.Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
self.frameUL = Tkinter.LabelFrame(self, text="Upper left")
self.frameUL.grid(row=0, column=0, sticky=W+E+N+S, padx=2, pady=2)
self.frameUR = Tkinter.LabelFrame(self, text="Upper right")
self.frameUR.grid(row=0, column=1, sticky=W+E+N+S, padx=2, pady=2)
self.frameLL = Tkinter.LabelFrame(self, text="Lower left")
self.frameLL.grid(row=1, column=0, sticky=W+E+N+S, padx=2, pady=2)
self.frameLR = Tkinter.LabelFrame(self, text="Lower right")
self.frameLR.grid(row=1, column=1, sticky=W+E+N+S, padx=2, pady=2)
self.scrollbarUR = Tkinter.Scrollbar(self.frameUR, orient='vertical')
self.scrollbarUR.grid(row=0, column=1, sticky=N+S)
self.textBoxUR = Tkinter.Text(self.frameUR)
self.textBoxUR.config(yscrollcommand=self.scrollbarUR.set)
self.textBoxUR.grid(row=0, column=0, sticky=W+E+N+S)
self.scrollbarUR.config(command=self.textBoxUR.yview)
self.scrollbarLLV = Tkinter.Scrollbar(self.frameLL, orient='vertical')
self.scrollbarLLV.grid(row=0, column=1, sticky=N+S)
self.scrollbarLLH = Tkinter.Scrollbar(self.frameLL, orient='horizontal')
self.scrollbarLLH.grid(row=1, column=0, sticky=W+E)
self.textBoxLL = Tkinter.Text(self.frameLL)
self.textBoxLL.config(wrap="none", yscrollcommand=self.scrollbarLLV.set, xscrollcommand=self.scrollbarLLH.set)
self.textBoxLL.grid(row=0, column=0, sticky=W+E+N+S)
self.scrollbarLLV.config(command=self.textBoxLL.yview)
self.scrollbarLLH.config(command=self.textBoxLL.xview)
self.scrollbarLRV = Tkinter.Scrollbar(self.frameLR, orient='vertical')
self.scrollbarLRV.grid(row=0, column=1, sticky=N+S)
self.scrollbarLRH = Tkinter.Scrollbar(self.frameLR, orient='horizontal')
self.scrollbarLRH.grid(row=1, column=0, sticky=W+E)
self.textBoxLR = Tkinter.Text(self.frameLR)
self.textBoxLR.config(wrap="none", yscrollcommand=self.scrollbarLRV.set, xscrollcommand=self.scrollbarLRH.set)
self.textBoxLR.grid(row=0, column=0, sticky=W+E+N+S)
self.scrollbarLRV.config(command=self.textBoxLR.yview)
self.scrollbarLRH.config(command=self.textBoxLR.xview)
for x in range(50):
self.textBoxUR.insert(END, str(x) + "\n")
for x in range(50):
self.textBoxLL.insert(END, str(x) + "\n")
for x in range(50):
self.textBoxLR.insert(END, str(x) + "\n")
if __name__ == "__main__":
app = findhash_tk(None)
app.title('Test App')
app.mainloop()
答案 0 :(得分:0)
在self.update()
之后粘贴:
self.textBoxUR.grid(row=0, column=0, sticky=W+E+N+S)
奇怪的是,进一步向下将无法工作(某些滚动条将显示为灰色,或者左下方水平将被着色,而不应该)。这将为你解决它,但它看起来像一个bug。
修改强> 正如OP提到的那样,这并不一致,反正也很奇怪。解决方法,在initialize()之后:
self.wm_geometry("+4000+4000")
self.after(500,lambda:self.wm_geometry("+10+10"))
这很脏,但是通过将窗口放在屏幕外来强制刷新窗口(如果你的屏幕大于4000像素哇,但你可以增加),并在半秒后将其移回。这看起来像一个奇怪的问题,并且在Linux上不会发生(尽管总是颜色是灰色的)。
答案 1 :(得分:0)
除Text之外,其他小部件也遇到了此错误。我想出的解决方案看起来更好,因为窗口移到屏幕外并再次移回时不会闪烁。以下解决方案可以检测滚动条何时从禁用状态变为活动状态,反之亦然,并销毁并重新创建滚动条。
from tkinter import *
if __name__ == "__main__":
root = Tk()
###
badframe = Frame(root)
badframe.grid(row=0, column=0)
badtext = Text(badframe)
badtext.grid(row=0, column=0)
badscrollbar = Scrollbar(badframe, command=badtext.yview)
badscrollbar.grid(row=0, column=1, sticky=N+S)
badtext.config(yscrollcommand=badscrollbar.set)
def fillbad():
global badtext
for i in range(50):
badtext.insert(END, str(i) + "\n")
badfillbutton = Button(root, command=fillbad, text="fill")
badfillbutton.grid(row=1, column=0)
def clearbad():
global badtext
badtext.delete(1.0, END)
badclearbutton = Button(root, command=clearbad, text="clear")
badclearbutton.grid(row=2, column=0)
###
def fixyscrollcommand(lo, hi):
global noyscroll, goodframe, goodscrollbar, goodtext
start = float(lo)
end = float(hi)
# weird Windows bug
if noyscroll != ((end - start) >= 1.0):
goodscrollbar.destroy()
goodscrollbar = Scrollbar(goodframe, command=goodtext.yview)
goodscrollbar.grid(row=0, column=1, sticky=N+S)
goodscrollbar.set(lo, hi)
noyscroll = False
if (end - start) >= 1.0:
noyscroll = True
goodframe = Frame(root)
goodframe.grid(row=0, column=1)
goodtext = Text(goodframe, yscrollcommand=fixyscrollcommand)
goodtext.grid(row=0, column=0)
goodscrollbar = Scrollbar(goodframe, command=goodtext.yview)
goodscrollbar.grid(row=0, column=1, sticky=N+S)
noyscroll = True
def fillgood():
global goodtext
for i in range(50):
goodtext.insert(END, str(i) + "\n")
goodbutton = Button(root, command=fillgood, text="fill")
goodbutton.grid(row=1, column=1)
def cleargood():
global goodtext
goodtext.delete(1.0, END)
goodclearbutton = Button(root, command=cleargood, text="clear")
goodclearbutton.grid(row=2, column=1)
###
mainloop()