Susan Susan - 11 months ago 71
Python Question

Python Tkinter - Tcl Error

I wrote this class with two methods:

import tkinter, random

class MojaGrafika:
def __init__(self):
self.canvas = tkinter.Canvas(width=400, height=300)

def text(self, text, x, y, farba=None):
self.x = x
self.y = y
self.t = text
self.canvas.create_text(self.x, self.y, text=self.t)

Then I run it (g = MojaGrafika(), g.text(200, 150, 'P', 'red'))
and this error came up:

Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
g.text(200, 150, 'P', 'red')
File "C:\Users\zuzha\Documents\Cvicenia z programovania 2016\ZS\", line 102, in text
self.p = self.canvas.create_text(self.x, self.y, text=self.t)
File "C:\Users\zuzha\AppData\Local\Programs\Python\Python35-32\lib\tkinter\", line 2344, in create_text
return self._create('text', args, kw)
File "C:\Users\zuzha\AppData\Local\Programs\Python\Python35-32\lib\tkinter\", line 2320, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: bad screen distance "P"

Can somebody help me with that?


Answer Source


You declare method with arguments

text(text, x, y, farba)

but then you call arguments in wrong order

text(x, y, text, farba)


g.text(200, 150, 'P', 'red')

so create_text() tries to use value P as x

(Polish) Zła kolejność argumentów