2016-11-13 140 views
-1

我写这个类两种方法:的Python的Tkinter - Tcl错误

import tkinter, random 

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

    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) 

然后我运行它(G = MojaGrafika(),g.text(200,150, 'P', '红') ) 和这个错误来了:

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\15.cvicenie.py", 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\__init__.py", line 2344, in create_text 
return self._create('text', args, kw) 
    File "C:\Users\zuzha\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2320, in _create 
    *(args + self._options(cnf, kw)))) 
_tkinter.TclError: bad screen distance "P" 

有人可以帮我吗?

感谢

回答

2

(英文)

你声明的方法与参数

text(text, x, y, farba) 

但然后调用参数顺序错误

text(x, y, text, farba) 

看到

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

所以create_text()尝试使用值P作为x


(波兰)ZLAkolejnośćargumentów

+1

是最后一句('PL:ZLAkolejnośćargumentów')答案不知何故一部分? –

+0

@BryanOakley是的它回答波兰语 - 我看到OP在代码中使用波兰语单词。 – furas

+0

是的,我刚刚发现了,谢谢! – Susan