[Tkinter] Writing something to a function.

Post here if you need help with creating a Graphical User Interface in Python.

[Tkinter] Writing something to a function.

Postby Colte » Mon Apr 29, 2013 6:03 pm

Hello,

I am new to phyton and I have a problem. I have been struggeling with it quite a while now and I am unable to resolve it. I have tryed looking on google with no result. Thats why I hope you guys can explain to me what I am doing wrong.

I want to write the coords from main.py to guiCode.py, so that I can create a line with the given coords.

main.py
Code: Select all
import guiCode
 
guiCode(10,10,20,20) #Writes the coords for the line to guiCode.createLine


guiCode.py
Code: Select all
from Tkinter import *
import Tkinter
import tkMessageBox

class guiCode():   

    root = Tkinter.Tk()
    cv1 = Tkinter.Canvas()
    cv1.pack(fill=BOTH, expand=YES)
   
    def createLine(self, x0, y0, x1, y1): #Function that creates a line with the given coords.
        self.cv1.create_line(x0, y0, x1, y1)
   
    clCreate = Tkinter.Button(text="Hello", command=createLine) #When the button is pressed the line will be created.
    clCreate.pack()
    root.mainloop()


error
Code: Select all
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
    return self.func(*args)
TypeError: createLine() takes exactly 5 arguments (0 given)
Traceback (most recent call last):
  File "C:\Users\Jeroen\Documents\Python\Naam\main.py", line 3, in <module>
    guiCode(10,10,20,20) #Writes the coords for the line to guiCode.createLine
TypeError: 'module' object is not callable


I hope that I have given enough information.

Thanks in advance,
Colte

EDIT: Error added, my apologies.
Last edited by Colte on Mon Apr 29, 2013 6:23 pm, edited 1 time in total.
Colte
 
Posts: 2
Joined: Mon Apr 29, 2013 5:58 pm

Re: [Tkinter] Writing something to a function.

Postby Yoriz » Mon Apr 29, 2013 6:17 pm

Hi ,
You didn't state what the problem was that you are getting from your code. edit thanks for adding your error

The TypeError: 'module' object is not callable is because you are importing the module then calling the module itself, you have your module name and the class inside the module named the same thing, Note class name should be camelCase and your module name would be better as lowercase.
Which would mean rename your class to GuiCode, then when you do the import it would be either
Code: Select all
from guicode import GuiCode
GuiCode()

or
Code: Select all
import guicode
guicode.GuiCode()


Note from looking over your code this is not the only thing wrong with it

Code: Select all
GuiCode(10,10,20,20)

You are creating an instance of GuiCode and passing in 4 parameters, but the class GuiCode has no __init__ method to accept those parameters.

I dont do Tkinter but ill bet that the callback createLine(self, x0, y0, x1, y1) cannot accept all those parameters, it probably only accepts a single event parameter

when you do command=createLine it should be command=self.createLine

im going to stop at that and say you need to learn how to use classes and then have another go, gui code is difficult you need to understand basic python first.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 566
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: [Tkinter] Writing something to a function.

Postby Colte » Mon Apr 29, 2013 6:49 pm

Yoriz wrote:---snip---


I am such an idiot, Thank you for this!

Colte.
Colte
 
Posts: 2
Joined: Mon Apr 29, 2013 5:58 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 1 guest

cron