Trying to learn TKinter in python

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2087 Views - Last Post: 16 May 2013 - 11:21 AM Rate Topic: -----

#1 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Trying to learn TKinter in python

Posted 06 May 2013 - 12:30 PM

Ok. So i am learning python and learned a bunch of cool things you can do with the console. I have made a few simple programs, two of which i want to get serious with and keep developing. One is a program for a local company. I thought it would be interesting to do and it is. I have turned it into a windows app (.exe), and it runs and drives great! Now I want to move on to making it look good. I have looked through the python documentation and seen tkinter. It gives you a small program (which i retyped below for practice), equivalent to the "Hello World" exercise:

import tkinter as tk


class Application(tk.Frame):
    def _init_(self, master = None):
        tk.Frame._init_(self, master)
        self.pack()
        self.createWidgets()

    def createWidgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(Click Me!)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side = "top")

        self.QUIT.tk.Button(self, text = "QUIT", fg = "red", command = root.destroy)

        self.QUIT.pack(side = "bottom")

    def say_hi(self):
        print ("Hi there, everyone!")


root = tk.Tk()
app = Application(master = root)
app.mainloop()



So after retyping this and checking if it was right i ran it. A blank window came up (labeled tk) with nothing in it. Can someone please explain why? And can someone help me learn tkinter, or at least teach me the basics?

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to learn TKinter in python

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3182
  • View blog
  • Posts: 10,659
  • Joined: 12-December 12

Re: Trying to learn TKinter in python

Posted 06 May 2013 - 01:39 PM

I wouldn't name the module tk, try tki so that it won't conflict with anything (hopefully!);
There are two underscore around the __init__ method - this is why nothing is happening, as this code isn't run;
TOP and BOTTOM are values of the tkinter module;
You have to assign QUIT on line 16.

Have a look through the tutorials section here @DIC, there is a link at the top of this page.

import tkinter as tki

class Application(tki.Frame):
    def __init__(self, master = None):
        tki.Frame.__init__(self, master)
        self.parent = master
        self.pack()
        self.createWidgets()

    def createWidgets(self):
        self.hi_there = tki.Button(self)
        self.hi_there["text"] = "Hello World\n(Click Me!)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side = tki.TOP)

        self.QUIT = tki.Button(self, text = "QUIT", fg = "red", command = root.destroy)

        self.QUIT.pack(side = tki.BOTTOM)

    def say_hi(self):
        print ("Hi there, everyone!")


root = tki.Tk()
app = Application(master = root)
app.mainloop()

This post has been edited by andrewsw: 06 May 2013 - 01:42 PM

Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3182
  • View blog
  • Posts: 10,659
  • Joined: 12-December 12

Re: Trying to learn TKinter in python

Posted 06 May 2013 - 04:05 PM

There is a sequence of Tkinter tutorials here @DIC, the first one. Click the Tutorials link at the top of the page to track down the others in the sequence.

I notice that these tutorials began in 2009 though, but I suspect they are still relevant(?).
Was This Post Helpful? 0
  • +
  • -

#4 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Trying to learn TKinter in python

Posted 07 May 2013 - 05:34 AM

I didn't think the underscores did anything. Especially seeing as that is how they had it in the documentation. Thanks for the pointer on the tutorials. I will begin on those in a bit. I will take out the underscores and add the top and bottom.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3182
  • View blog
  • Posts: 10,659
  • Joined: 12-December 12

Re: Trying to learn TKinter in python

Posted 07 May 2013 - 06:07 AM

View Posttpaskell, on 07 May 2013 - 12:34 PM, said:

I didn't think the underscores did anything. Especially seeing as that is how they had it in the documentation. Thanks for the pointer on the tutorials. I will begin on those in a bit. I will take out the underscores and add the top and bottom.

Take them out? :dontgetit: They need four underscores, two on each side! These are magic methods, aka dunder (double-underscore) methods, that have a special meaning to Python. __init__ is the method that Python will run when an object is instantiated. [Actually, there is one that is run before this, called __new__, but this is on a need to know basis :)]

This post has been edited by andrewsw: 07 May 2013 - 06:08 AM

Was This Post Helpful? 0
  • +
  • -

#6 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Trying to learn TKinter in python

Posted 07 May 2013 - 07:11 AM

Ok in sorry I misunderstood you.I will add and then see what happens. Is the "dunder" used with other gui modules too? Or is it unique to tkinter?
Was This Post Helpful? 0
  • +
  • -

#7 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Trying to learn TKinter in python

Posted 07 May 2013 - 07:45 AM

Ok so I got it to work. I cleaned up the code and made it more readable (to me). So how do I include an input field where I can ask the user questions and according to their response print (to the application) the answer followed by the next question. I have all this written in python code (printing to console), but I want to eventually create a .exe application where python is not needed.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3182
  • View blog
  • Posts: 10,659
  • Joined: 12-December 12

Re: Trying to learn TKinter in python

Posted 07 May 2013 - 07:59 AM

View Posttpaskell, on 07 May 2013 - 02:11 PM, said:

Ok in sorry I misunderstood you.I will add and then see what happens. Is the "dunder" used with other gui modules too? Or is it unique to tkinter?

It is not unique to tkinter, but it is fundamental to a proper understanding of Python, which is an object-oriented programming language. I recommend that you pause exploring tkinter and study Python. In particular:

do you fully understand the code in your first post? (Or, more accurately, my corrected version?)

If you don't understand it you are not going to be able to modify, or add-to, it.

This post has been edited by andrewsw: 07 May 2013 - 08:03 AM

Was This Post Helpful? 0
  • +
  • -

#9 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Trying to learn TKinter in python

Posted 07 May 2013 - 09:42 AM

I understand most of it. I will do a little more reading on the different functions.
Was This Post Helpful? 0
  • +
  • -

#10 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Trying to learn TKinter in python

Posted 07 May 2013 - 01:19 PM

I am going through through those tutorials you referenced and am working on number 3. I am having a problem though. When i import tkFileDialog i get the error that it isn't a module. I looked up a test to see if it is there:


import tkinter as tk
print(tk.filedialog.__doc__)




After running this it did at it should. That tells me the module is there but i cant import it. Any ideas?
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3182
  • View blog
  • Posts: 10,659
  • Joined: 12-December 12

Re: Trying to learn TKinter in python

Posted 07 May 2013 - 01:35 PM

tkFileDialog is now (apparently) tkinter.filedialog - as you have discovered, so you no longer need to import it separately.

I did mention that that tutorial was begun in 2009, so you may come across one or two little differences, such as this.

This post has been edited by andrewsw: 07 May 2013 - 01:36 PM

Was This Post Helpful? 0
  • +
  • -

#12 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Trying to learn TKinter in python

Posted 07 May 2013 - 01:53 PM

Ok thanks for the help. I'm headed to work now but will try it tonight.
Was This Post Helpful? 0
  • +
  • -

#13 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Trying to learn TKinter in python

Posted 08 May 2013 - 12:20 PM

OK. So after working with it a bit, i came to a problem that i cant seem to find the answer for. This is what i have so far:


from tkinter import *

print ("Application has started")


class Application():
    def __init__(self):
        self.root = Tk()
        self.root.title = ("Driller")
        self.root.minsize(width = 500, height = 400)
        self.root.maxsize(width = 900, height = 800)
        
        self.createWidgets()
        

        menubar = Menu(self.root)
        # File Menu
        file = Menu(menubar, tearoff = 0)
        file.add_command(label = "New File", accelerator = "Ctrl+N")
        file.add_command(label = "Open", accelerator = "Ctrl+O")
        file.add_separator()
        file.add_command(label = "Save", accelerator = "Ctrl+S")
        file.add_command(label = "Save As", accelerator = "Ctrl+Shift+S")
        file.add_separator()
        file.add_command(label = "Print", accelerator = "Ctrl+P")
        file.add_separator()
        file.add_command(label = "Quit", accelerator = "Ctrl+Q")
        menubar.add_cascade(label = "File", menu = file)
        # Edit Menu
        edit = Menu(menubar, tearoff = 0)
        edit.add_command(label = "Find", accelerator = "Ctrl+F")
        edit.add_separator()
        edit.add_command(label = "Cut", accelerator = "Ctrl+X")
        edit.add_command(label = "Copy", accelerator = "Ctrl+C")
        edit.add_command(label = "Paste", accelerator = "Ctrl+V")
        edit.add_separator()
        edit.add_command(label = "Undo", accelerator = "Ctrl+Z")
        menubar.add_cascade(label = "Edit", menu = edit)
        
        self.root.config(menu = menubar)
        
        # Buttons
    def createWidgets(self):
        self.twoinch_button = tk.Button(self)
        self.twoinch_button ["text"] = "Two Inch"
        self.twoinch_button ["command"] = self.twoinch

    def twoinch(self):
        print ("TEST")
        


app = Application()

app.root.mainloop()
 



The problem i am having, is that when i try to call createWidgets in __init__ i get the error global name 'tk' not defined. When i look at the error it goes straight to the button made in createWidgets. I know it is probably something simple, but i have been tinkering with it for a while now. Any Ideas?

This post has been edited by tpaskell: 08 May 2013 - 12:29 PM

Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3182
  • View blog
  • Posts: 10,659
  • Joined: 12-December 12

Re: Trying to learn TKinter in python

Posted 08 May 2013 - 12:48 PM

Well I got it to work with the following edits:

class Application(Frame):
    def __init__(self):
    ...

    def createWidgets(self):
        self.twoinch_button = Button()
        self.twoinch_button["text"] = "Two Inch"
        self.twoinch_button["command"] = self.twoinch
        self.twoinch_button.pack()

But I've no idea what I'm doing, I just made this up! You should continue to study and understand why these edits seem to work, and whether they are even correct.
Was This Post Helpful? 0
  • +
  • -

#15 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Trying to learn TKinter in python

Posted 08 May 2013 - 07:21 PM

So what i am seeing, is that you used Button() instead of tk.button and you packed it, which i didn't. I didn't realize packing was part of the process. I will try editing these two things and see what happens.

EDIT: I fixed those two and that worked perfect! Thank you. I will continue to build and see how far i get.

This post has been edited by tpaskell: 08 May 2013 - 07:23 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2