Trying to learn TKinter in python

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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 09 May 2013 - 07:16 AM

Ok Now that i have created my buttons and my menus, how do i make it put new buttons and text on the current window, after a button is pushed? I know i have to include them in the handlers for the button, i just don't know what to include.

EDIT: And why would my title still be "tk" instead of Driller as declared in

self.root.title = "Driller"

or

self.root.title = ("Driller")



?

This post has been edited by tpaskell: 09 May 2013 - 07:52 AM

Was This Post Helpful? 0
  • +
  • -

#17 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 09 May 2013 - 07:45 PM

So I have written a lot more code. I added a lot to what happens when a certain button is pushed. But it just builds down adding to the bottom. How do I make it to where I push a button, all the other options will go away and only present what the button commands?
Was This Post Helpful? 0
  • +
  • -

#18 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 10 May 2013 - 04:28 PM

Can anyone help me with these issues?
Was This Post Helpful? 0
  • +
  • -

#19 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 16 May 2013 - 10:38 AM

So no one then?
Was This Post Helpful? 0
  • +
  • -

#20 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7952
  • View blog
  • Posts: 13,547
  • Joined: 19-March 11

Re: Trying to learn TKinter in python

Posted 16 May 2013 - 11:15 AM

EDIT: I posted this without noticing that I was behind in the thread. This follows the conversation from the previous page - you're past this, but it might still be useful to you, so I'll leave it.


Okay, so this is an object lesson in namespaces.

You've imported Tkinter.* - this means you have all of the TKinter classes in your namespace, naked - they have the same status as locally defined functions and builtin functions. If you're in the the interpreter, you can type dir() and you'll see a lot of functions and values defined - about 180 of them, all together, most of which come from tkinter.
This has the advantage of reducing the typing you have to do - you don't need to call the functions by referencing a namespace. However, it also means that you have to be careful about what names you use - for example, 'X' is taken, and if you do something like

X="the spot"



you'll break some stuff, and it'll be a pain to figure out.

Some people will suggest that you import classes as somename, for example

[/code]
import Tkinter as tk
[/code]

If you do this, then you'd call the Tkinter classes via that name, which is what it looks like you're trying to do in the problem line above. The advantages and disadvantages of this are the converse of those above: you have to type a little more, but you've got a little insulation against bugs of that nature. This also helps to provide a little hard documentation - every Tkinter class is labeled when you use it. This is convenient if you have multiple namespaces that you're working with.

There's also the compromise of importing just what you need:

from Tkinter import Button, Menu



This allows you to call just those classes "naked", and the rest of Tkinter is not visible to your current code. You could even combine these:

from Tkinter import Button, Menu
import Tkinter as tk



Now you can refer to Button and Menu on their own, and also tk.Scrollbar or tk.Frame

Which one of these is the "right" way to do it depends on your style, how complicated your code is, and how well the library is written. Tkinter is pretty good, so you're probably fine using a blanket import, IF you're separating out your graphics code from the rest of the world - but this takes us into design patterns, and I'm not going there today. (Google Model, View Controller Pattern if you want to chase down that road)

I suspect that you're using a few different tutorials to learn from, which is good. Keep an eye on how they do their imports, and modify the code you see to suit the way you're doing things.

This post has been edited by jon.kiparsky: 16 May 2013 - 11:16 AM

Was This Post Helpful? 0
  • +
  • -

#21 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7952
  • View blog
  • Posts: 13,547
  • Joined: 19-March 11

Re: Trying to learn TKinter in python

Posted 16 May 2013 - 11:21 AM

View Posttpaskell, on 09 May 2013 - 09:45 PM, said:

So I have written a lot more code. I added a lot to what happens when a certain button is pushed. But it just builds down adding to the bottom. How do I make it to where I push a button, all the other options will go away and only present what the button commands?


I'm not a Tkinter guru, I've only played with it a little bit, but one approach that you might consider is to put you functionality in some sort of container, and rebuild that container when you want to change the display. This way you can leave the rest of your window in place, and minimize the work you have to do.

But maybe if you post what you're looking at now, and try to describe what you're going for, someone will be able to be more helpful than this.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2