Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tkinter scrollbar issue

I am recieving a strange bahavior and exception of the Tkinter scrollbar. My GUI basically uses a Tkinter Text Widget which is related/referenced to the scrollbar.

    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")

In the Text Widget itself python console entries are inserted using sys.stdoutand sys.stderr. To insert the text a support class is used which overwrites the .sys.stderr.writeor printmethod and writes the text into the Tkinter text widget.

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")

Inserting the text into the Text Widget works as expected, also clicking into the Widget and scrolling using the mouse wheel works fine, also using the up and down buttons of the scrollbar works. All trouble starts using the bar and trying to slide up and down. The effect is a 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"
like image 903
freeski_52 Avatar asked Jul 07 '26 22:07

freeski_52


1 Answers

So, I just found a kind of workaround solution to my problem Tkinter seems to have problems concerning multple threads. So I just changed the Tkinter.Scrollbar to ttk.Scrollbar. The ttk module is a extended version of Tkinter and solves some weird behaviors of the Tkinter module.

Look at http://docs.python.org/3.1/library/tkinter.ttk.html

After changing the module of the widget everything works as expected!

This solution is just a workaround of the actual issue but it works!!

like image 147
freeski_52 Avatar answered Jul 11 '26 07:07

freeski_52



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!