Page 1 of 1

[Solved] Learn Python Programming

Posted: 16 Aug 2014 22:25
by jsvenancio
Good evening.

How do I install correctly with Python Tkinter?
I'm using Geany to learn to program in Python, I'm trying to start work the following code:

Code: Select all

from Tkinter import *

def Cumprimente():
	hello.set("Hello World")

gui = tk()

gui.title("Python3 + tkinter")
gui.geometry("400x300")

btn = Button(gui, text="Cumprimente", Command=Cumprimente)
btn.pack()

hello = StringVar()
lbl = Label(gui, TextVariable=hello)
lbl.pack()

gui.mainloop()
But whenever I try to run the code I get this error:

Code: Select all

Traceback (most recent call last):
  File "Hello World.py", line 7, in <module>
    gui = tk()
NameError: name 'tk' is not defined


------------------
(program exited with code: 1)
Press return to continue
I think the problem is related to the Tkinter.
How can I solve this?

Re: Learn Python Programming

Posted: 22 Aug 2014 18:01
by wildman
Make sure these packages are installed:

python
python-tk
python-pmw
python-imaging
idle
tk
tcl

Re: Learn Python Programming

Posted: 24 Aug 2014 14:09
by jsvenancio
wildman wrote: tk
The tk package, I can not find it in the manager SolydX programs.

Re: Learn Python Programming

Posted: 24 Aug 2014 15:30
by wildman
The tk package, I can not find it in the manager SolydX programs.
That is odd. I am using SolydX and tk was installed by default. Here is a screen shot of Synaptic:
https://www.dropbox.com/s/gpl2i14yrk7re ... c.png?dl=0

What do you get if you enter this into a terminal?

Code: Select all

apt policy tk

Re: Learn Python Programming

Posted: 24 Aug 2014 16:24
by Deleted User 2764
Same here on a SolydK system:

Code: Select all

ravenlx@solydk-hp ~ $ apt policy tk
tk:
  Installed: 8.6.0+8
  Candidate: 8.6.0+8
  Version table:
 *** 8.6.0+8 0
        500 http://debian.solydxk.com/production/ testing/main amd64 Packages
        100 /var/lib/dpkg/status
It should already be installed.

Re: Learn Python Programming

Posted: 25 Aug 2014 14:50
by jsvenancio
wildman wrote:What do you get if you enter this into a terminal?

Code: Select all

apt policy tk
It appears that is already installed.

Code: Select all

$ apt policy tk
tk:
  Instalado: 8.6.0+8
  Candidato: 8.6.0+8
  Tabela de Versão:
 *** 8.6.0+8 0
        500 http://debian.solydxk.com/production/ testing/main i386 Packages
        100 /var/lib/dpkg/status
$ 
But whenever I try to run the code I get this error:

Code: Select all

Traceback (most recent call last):
  File "Hello World.py", line 7, in <module>
    gui = tk()
NameError: name 'tk' is not defined


------------------
(program exited with code: 1)
Press return to continue

Re: Learn Python Programming

Posted: 25 Aug 2014 15:28
by Deleted User 2764
First, you need to:

Code: Select all

sudo apt-get install python-tk
In your code, use:

Code: Select all

from Tkinter import *
and

Code: Select all

gui = Tk()
with an upper case T.

It may not work either. Try reading this (it's in English so you'll need to translate it):

https://wiki.python.org/moin/TkInter

Also after I did the above changes, I got this:

Code: Select all

Traceback (most recent call last):
  File "./HelloWorld.py", line 13, in <module>
    btn = Button(gui, text="Cumprimente", Command=Cumprimente)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2128, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2058, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-Command"
Which you'll need to fix yourself. It seems maybe the command or main isn't working? I don't know python enough to know exactly what is going on though.

Note that I do not use spaces in the filename. I don't find it's a good idea to put spaces in filenames as it can sometimes make it harder to execute in Linux (ie. need to use a \ before a space in the command line, for example).

Re: Learn Python Programming

Posted: 25 Aug 2014 15:39
by Deleted User 2764
Ok, after doing the installing of python-tk, try this code:

Code: Select all

from Tkinter import *

def Pressed():                          #function
        print 'buttons are cool'

root = Tk()                             #main window
button = Button(root, text = 'Press', command = Pressed)
button.pack(pady=20, padx = 20)
Pressed()
root.mainloop()
It should work fine. It may too be that your code may have some bugs in it. Remember, python is also very case sensitive.

Here's where I got the above code from: https://wiki.python.org/moin/Intro%20to ... %20Tkinter

It might be a nice tutorial for those learning Python/Tk.

Re: Learn Python Programming

Posted: 25 Aug 2014 15:43
by Deleted User 2764
I have debugged your original code. Try this:

Code: Select all

#!/usr/bin/env python

from Tkinter import *

def Cumprimente():
   hello.set("Hello World")

gui = Tk()

gui.title("Python3 + tkinter")
gui.geometry("400x300")

btn = Button(gui, text="Cumprimente", command=Cumprimente)
btn.pack()

hello = StringVar()
lbl = Label(gui, textvariable=hello)
lbl.pack()

gui.mainloop()
I changed "Tkinter, Tk, and also "Command" needed to be "command" as well as "TextVariable" needed to be "textvariable".

Worked on my system after installing "python-tk". Hope this helps!

Re: Learn Python Programming

Posted: 25 Aug 2014 17:07
by jsvenancio
RavenLX wrote:I have debugged your original code. Try this:

Code: Select all

#!/usr/bin/env python

from Tkinter import *

def Cumprimente():
   hello.set("Hello World")

gui = Tk()

gui.title("Python3 + tkinter")
gui.geometry("400x300")

btn = Button(gui, text="Cumprimente", command=Cumprimente)
btn.pack()

hello = StringVar()
lbl = Label(gui, textvariable=hello)
lbl.pack()

gui.mainloop()
I changed "Tkinter, Tk, and also "Command" needed to be "command" as well as "TextVariable" needed to be "textvariable".
Thanks for your help.
This code works, the error was mine, wrote with capital letter when he should write with lowercase letter.

I will also analyze the tutorials you mentioned.

Re: [Solved] Learn Python Programming

Posted: 22 Jan 2019 13:28
by liam10
Please follow this procedure:
Install Python. According to python.org, ...
Install Tkinter. To see if you have Tkinter, launch python; then at the Python prompt, type >>> import Tkinter. ...




Python Tutorial