我收到了一个奇怪的行为和Tkinter 滚动条的例外。我的 GUI 基本上使用与滚动条相关/引用的 Tkinter文本小部件。
self.textFrame = Tkinter.LabelFrame (self.mainFrame,padx=0,pady=0,width=200,height=100)
self.textFrame.grid(row=5, column =1, sticky = "NW", padx = 5, pady = 10)
self.consLable = Tkinter.Label (self.textFrame,text = "Log-Console:",font ="Verdana 8 bold")
self.consLable.grid (row =6,column =1, sticky = "NW", padx = 5, pady = 1)
self.consText= Tkinter.Text(self.textFrame, wrap = "word")
self.consText.grid(row =7,column =1, rowspan =4)
self.consText.tag_configure("stderr", foreground="#b22222")
self.scrollText= Tkinter.Scrollbar(self.textFrame,command = self.consText.yview)
self.scrollText.grid(row =7,column =2,rowspan =4,sticky='NSEW')
self.consText.config(yscrollcommand = self.scrollText.set)
# Referencing output location of the console "print or sys.stderr" methods
sys.stdout = gemeindesteckbrief__SupportTools__.TextRedirector(self.consText, "stdout")
sys.stderr= gemeindesteckbrief__SupportTools__.TextRedirector(self.consText, "stderr")
在文本小部件本身中, python 控制台条目sys.stdout
是使用和插入的sys.stderr
。为了插入文本,使用了一个支持类,它覆盖了.sys.stderr.write
orprint
方法并将文本写入 Tkinter 文本小部件。
class TextRedirector(object):
def __init__(self,widget, tag):
self.targetwidget = widget
self.targettag = tag
#@Override the sys.stdout & sys.stderr methods to write to the text widget instead of the python console
def write(self, str):
self.targetwidget.configure(state="normal")
self.targetwidget.insert("end", str, (self.targettag,))
self.targetwidget.configure(state="disabled")
将文本插入文本小部件按预期工作,单击小部件并使用鼠标滚轮滚动也可以正常工作,也可以使用滚动条的向上和向下按钮。所有的麻烦都开始使用酒吧并试图上下滑动。效果是TclError: expected floating-point number but got "0,0028"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python26\ArcGIS10.0\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Python26\ArcGIS10.0\lib\lib-tk\Tkinter.py", line 3156, in yview
self.tk.call((self._w, 'yview') + what)
TclError: expected floating-point number but got "0,0028"