[Solved] Learn Python Programming

Questions about software.
User avatar
jsvenancio
Posts: 195
Joined: 16 Dec 2013 17:59
Location: Portugal
Contact:

[Solved] Learn Python Programming

Postby jsvenancio » 16 Aug 2014 22:25

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?
SolydX - 64bts - Debian

Sorry for my bad English, I speak Portuguese and use the Google translator.

User avatar
wildman
Posts: 244
Joined: 12 Feb 2014 23:31
Location: Kentucky, USA

Re: Learn Python Programming

Postby wildman » 22 Aug 2014 18:01

Make sure these packages are installed:

python
python-tk
python-pmw
python-imaging
idle
tk
tcl
Wildman
May the Source be with you.

User avatar
jsvenancio
Posts: 195
Joined: 16 Dec 2013 17:59
Location: Portugal
Contact:

Re: Learn Python Programming

Postby jsvenancio » 24 Aug 2014 14:09

wildman wrote: tk
The tk package, I can not find it in the manager SolydX programs.
SolydX - 64bts - Debian

Sorry for my bad English, I speak Portuguese and use the Google translator.

User avatar
wildman
Posts: 244
Joined: 12 Feb 2014 23:31
Location: Kentucky, USA

Re: Learn Python Programming

Postby wildman » 24 Aug 2014 15:30

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
Wildman
May the Source be with you.

Deleted User 2764

Re: Learn Python Programming

Postby Deleted User 2764 » 24 Aug 2014 16:24

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.

User avatar
jsvenancio
Posts: 195
Joined: 16 Dec 2013 17:59
Location: Portugal
Contact:

Re: Learn Python Programming

Postby jsvenancio » 25 Aug 2014 14:50

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
SolydX - 64bts - Debian

Sorry for my bad English, I speak Portuguese and use the Google translator.

Deleted User 2764

Re: Learn Python Programming

Postby Deleted User 2764 » 25 Aug 2014 15:28

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

Deleted User 2764

Re: Learn Python Programming

Postby Deleted User 2764 » 25 Aug 2014 15:39

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.

Deleted User 2764

Re: Learn Python Programming

Postby Deleted User 2764 » 25 Aug 2014 15:43

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!

User avatar
jsvenancio
Posts: 195
Joined: 16 Dec 2013 17:59
Location: Portugal
Contact:

Re: Learn Python Programming

Postby jsvenancio » 25 Aug 2014 17:07

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.
SolydX - 64bts - Debian

Sorry for my bad English, I speak Portuguese and use the Google translator.

liam10
Posts: 1
Joined: 22 Jan 2019 13:26

Re: [Solved] Learn Python Programming

Postby liam10 » 22 Jan 2019 13:28

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


Return to “Software”

Who is online

Users browsing this forum: No registered users and 0 guests