By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,212 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,212 IT Pros & Developers. It's quick & easy.

Please help - Tkinter not doing anything

P: n/a
Hello. I'm a complete newbie trying to learn Python. I decided to try
some Tkinter examples, including the one from the library reference,
but they don't seem to do anything! Shouldn't there be, like, a
dialog?

I'm running Windows XP and using IDLE. You can assume my version of
Python is the latest.
Jun 27 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Try adding:

from Tkinter import *

at the beginning, and you don't need "var" in front of root=Tk(), just
"root = Tk()" (<-without the quotes of course)
What OS are you on? Are you running "python testapp.py" or similar to
make it run?

-Chuckk

On Sun, May 4, 2008 at 12:39 PM, Protected <pr*******@myshelter.netwrote:
Good thinking. It was indented with spaces, so I replaced them with tabs.
Now I'm getting a SyntaxError: invalid syntax in root = Tk(). If I split the
code in two parts (with the second one beginning in that line) and run them
separately, I get no errors, but still nothing happens.

class Application(Frame):
def say_hi(self):
print "hi there, everyone!"

def createWidgets(self):
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit

self.QUIT.pack({"side": "left"})

self.hi_there = Button(self)
self.hi_there["text"] = "Hello",
self.hi_there["command"] = self.say_hi

self.hi_there.pack({"side": "left"})

def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()

var root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()

On Sun, May 4, 2008 at 12:33 PM, Q4 <q4@invalid.comwrote:
On Sun, 4 May 2008 02:23:37 -0700 (PDT), Protected <my*******@gmail.com>
wrote:
Hello. I'm a complete newbie trying to learn Python. I decided to try
some Tkinter examples, including the one from the library reference,
but they don't seem to do anything! Shouldn't there be, like, a
dialog?
>
I'm running Windows XP and using IDLE. You can assume my version of
Python is the latest.
--
http://mail.python.org/mailman/listinfo/python-list
If you just copy the code from the python doc the indentation might be
broken.
Send the code and I'll take a look at it.

Do you get any errors?

--
My website:
http://www.riddergarn.dk/koder/


--
http://mail.python.org/mailman/listinfo/python-list


--
http://www.badmuthahubbard.com
Jun 27 '08 #2

P: n/a
I had previously ran the import line. I prepended it to the example
code I'm trying to run every time but it did not help, still nothing
happens. With or without var before 'root'. I'm pasting the code in
IDLE and using Windows XP as written in the first post.

On May 4, 11:04 am, "Chuckk Hubbard" <badmuthahubb...@gmail.com>
wrote:
Try adding:

from Tkinter import *

at the beginning, and you don't need "var" in front of root=Tk(), just
"root = Tk()" (<-without the quotes of course)
What OS are you on? Are you running "python testapp.py" or similar to
make it run?

-Chuckk

On Sun, May 4, 2008 at 12:39 PM, Protected <protec...@myshelter.netwrote:
Good thinking. It was indented with spaces, so I replaced them with tabs.
Now I'm getting a SyntaxError: invalid syntax in root = Tk(). If I split the
code in two parts (with the second one beginning in that line) and run them
separately, I get no errors, but still nothing happens.
class Application(Frame):
def say_hi(self):
print "hi there, everyone!"
def createWidgets(self):
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit
self.QUIT.pack({"side": "left"})
self.hi_there = Button(self)
self.hi_there["text"] = "Hello",
self.hi_there["command"] = self.say_hi
self.hi_there.pack({"side": "left"})
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
var root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()
On Sun, May 4, 2008 at 12:33 PM, Q4 <q...@invalid.comwrote:
On Sun, 4 May 2008 02:23:37 -0700 (PDT), Protected <myshel...@gmail.com>
wrote:
Hello. I'm a complete newbie trying to learn Python. I decided to try
some Tkinter examples, including the one from the library reference,
but they don't seem to do anything! Shouldn't there be, like, a
dialog?
I'm running Windows XP and using IDLE. You can assume my version of
Python is the latest.
--
http://mail.python.org/mailman/listinfo/python-list
If you just copy the code from the python doc the indentation might be
broken.
Send the code and I'll take a look at it.
Do you get any errors?
--
My website:
http://www.riddergarn.dk/koder/
--
http://mail.python.org/mailman/listinfo/python-list

--http://www.badmuthahubbard.com
Jun 27 '08 #3

P: n/a
On May 4, 5:22 am, Protected <myshel...@gmail.comwrote:
I had previously ran the import line. I prepended it to the example
code I'm trying to run every time but it did not help, still nothing
happens. With or without var before 'root'. I'm pasting the code in
IDLE and using Windows XP as written in the first post.
Tkinter doesn't work if you type the statements in IDLE. I don't
remember the specifics of it, but essentially it doesn't work because
IDLE is itself a Tkinter app. You have to type it at the Python
command line or save it in a file. BTW, if you're on Windows, you
should definitely check wxPython. I used to work with Tkinter,
struggling with it all the time only to get a lame result most of the
time. Then I switched to wxPython, and in the same week I was learning
it I did a better GUI than I ever did in Tkinter (with months of
work!). I feel it makes it easier to make your program have a better
structure and design, and thus lets you focus on the actual task of
the program, rather than in the GUI itself. Plus, on Windows, you'll
get the widgets to look more natively, like any good quality
application there is on Windows.
Jun 27 '08 #4

P: n/a
On May 4, 12:18 pm, s0s...@gmail.com wrote:
On May 4, 5:22 am, Protected <myshel...@gmail.comwrote:
I had previously ran the import line. I prepended it to the example
code I'm trying to run every time but it did not help, still nothing
happens. With or without var before 'root'. I'm pasting the code in
IDLE and using Windows XP as written in the first post.

Tkinter doesn't work if you type the statements in IDLE. I don't
remember the specifics of it, but essentially it doesn't work because
IDLE is itself a Tkinter app. You have to type it at the Python
command line or save it in a file. BTW, if you're on Windows, you
should definitely check wxPython. I used to work with Tkinter,
struggling with it all the time only to get a lame result most of the
time. Then I switched to wxPython, and in the same week I was learning
it I did a better GUI than I ever did in Tkinter (with months of
work!). I feel it makes it easier to make your program have a better
structure and design, and thus lets you focus on the actual task of
the program, rather than in the GUI itself. Plus, on Windows, you'll
get the widgets to look more natively, like any good quality
application there is on Windows.
Ah, but is it cross platform?

(Thanks for letting me know about IDLE.)
Jun 27 '08 #5

P: n/a
WxWidgets, Tkinter, PyQT are all cross platform. Also have a look at
http://wiki.python.org/moin/GuiProgramming
for more GUI frameworks.

RCB
>On May 4, 4:59*am, Protected <myshel...@gmail.comwrote:
On May 4, 12:18 pm, s0s...@gmail.com wrote:


On May 4, 5:22 am, Protected <myshel...@gmail.comwrote:
I had previously ran the import line. I prepended it to the example
code I'm trying to run every time but it did not help, still nothing
happens. With or without var before 'root'. I'm pasting the code in
IDLE and using Windows XP as written in the first post.
Tkinter doesn't work if you type the statements in IDLE. I don't
remember the specifics of it, but essentially it doesn't work because
IDLE is itself a Tkinter app. You have to type it at the Python
command line or save it in a file. BTW, if you're on Windows, you
should definitely check wxPython. I used to work with Tkinter,
struggling with it all the time only to get a lame result most of the
time. Then I switched to wxPython, and in the same week I was learning
it I did a better GUI than I ever did in Tkinter (with months of
work!). I feel it makes it easier to make your program have a better
structure and design, and thus lets you focus on the actual task of
the program, rather than in the GUI itself. Plus, on Windows, you'll
get the widgets to look more natively, like any good quality
application there is on Windows.

Ah, but is it cross platform?

(Thanks for letting me know about IDLE.)- Hide quoted text -

- Show quoted text -
Jun 27 '08 #6

P: n/a
s0****@gmail.com wrote:
On May 4, 5:22 am, Protected <myshel...@gmail.comwrote:
>.... I'm pasting the code in IDLE and using Windows XP....

Tkinter doesn't work if you type the statements in IDLE.... it doesn't
work because IDLE is itself a Tkinter app.
Actually, _because_ IDLE is a Tkinter app,you can use it to
experiment with Tkinter in a very useful way. The trick is that
you need to start idle with the "-n" (No subprocesses) switch.
This is so useful that I build a shortcut on my desktop to do exactly
this. Using this shortcut you can see the effect of each action as
you type it, giving you an idea of exactly what must happen.

I'm assuming you are using Python 2.5 here (other versions have
related recipes, but slightly more complicated).
Right click on the desktop, and choose "New Shortcut."

For "Choose the location of the item", browse to (or type in)
C:\Python25\pythonw.exe
Then (after your next) pick a name for your shortcut.
Right click on the resulting shortcut, and go to "properties"
Change the "Target" entry from:
C:\Python25\pythonw.exe
to:
C:\Python25\pythonw.exe -m idlelib.idle -n
Also, if you are like me, you'll want to change the "Start in" directory
to wherever you work on python code --so imports of your own stuff "just
work", and so "File open'" and "Save As" default to a "nice" directory.

The Idle you get with this "corrupts" more easily, since the "-n" says
"no subprocess." Things like restarting the shell don't work. _But_
Tkinter stuff is available (there is already a running mainloop). You
can interactively do simple things a step at a time and watch the
results. It is a _great_ way to experiment with Tkinter.
--Scott David Daniels
Sc***********@Acm.Org

Jun 27 '08 #7

P: n/a
On May 4, 6:59 am, Protected <myshel...@gmail.comwrote:
On May 4, 12:18 pm, s0s...@gmail.com wrote:
On May 4, 5:22 am, Protected <myshel...@gmail.comwrote:
I had previously ran the import line. I prepended it to the example
code I'm trying to run every time but it did not help, still nothing
happens. With or without var before 'root'. I'm pasting the code in
IDLE and using Windows XP as written in the first post.
Tkinter doesn't work if you type the statements in IDLE. I don't
remember the specifics of it, but essentially it doesn't work because
IDLE is itself a Tkinter app. You have to type it at the Python
command line or save it in a file. BTW, if you're on Windows, you
should definitely check wxPython. I used to work with Tkinter,
struggling with it all the time only to get a lame result most of the
time. Then I switched to wxPython, and in the same week I was learning
it I did a better GUI than I ever did in Tkinter (with months of
work!). I feel it makes it easier to make your program have a better
structure and design, and thus lets you focus on the actual task of
the program, rather than in the GUI itself. Plus, on Windows, you'll
get the widgets to look more natively, like any good quality
application there is on Windows.

Ah, but is it cross platform?

(Thanks for letting me know about IDLE.)
Of course. I forgot to say about that. It is cross platform, like most
things in Python. I'd say it's actually more cross platform than
Tkinter, because of the looks of both in different platforms. In
Tkinter, you can get all the widgets to work in all the platforms it
supports, but the widgets look so different on different platforms,
that at the end you end up modifying the code anyway so that the
controls look better on the platform that you port the program to.
With what I've done with wxPython, the controls look great on any of
the platforms I've tried my programs in. Another thing: wxPython is a
port of the wxWidgets toolkit which is for C++. It has also been
ported to a bunch of other languages like Ruby, Perl, C#, etc; so if
you learn any of the languages which wxWidgets has been ported to, you
can use the toolkit in the same way that you've been doing with
Python, except that with the syntax of the other language.
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.