newbee needs help.

This is the place for queries that don't fit in any of the other categories.

newbee needs help.

Postby elbeardo » Wed Dec 18, 2013 1:46 pm

I have this code:

Code: Select all
from tkinter import *

text = 1

def blaha():
    global text
    text = text  + 1
    return text;


window = Tk()
Label(text=text).pack()
button = Button(window, text = '+ 1', command = blaha() )
button.pack()


mainloop()





I am trying to get the "text label" to count upp on buttonpress. Am i using the wrong widget or... its probably a bunsh of syntax-errors.
Would be nice to keep it simple with no class stuff for now, =)
Last edited by Mekire on Thu Dec 19, 2013 8:48 am, edited 1 time in total.
Reason: First post lock.
elbeardo
 
Posts: 5
Joined: Wed Dec 18, 2013 1:39 pm

Re: newbee needs help.

Postby stranac » Wed Dec 18, 2013 2:08 pm

If there were syntax errors in your code, it wouldn't run at all.

The problem is: you are assigning new values to text, but that doesn't change the text of the label.
If you want the text to update automatically, you'll need to use a StringVar.

http://effbot.org/tkinterbook/label.htm wrote:You can associate a Tkinter variable with a label. When the contents of the variable changes, the label is automatically updated:

Code: Select all
v = StringVar()
Label(master, textvariable=v).pack()

v.set("New Text!")


That return line in the blaha() function is not needed.
Also, please no semicolons at the end of lines...

And I would strongly recommend not using star imports.
Better to use:
Code: Select all
import tkinter

tkinter.Label...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: newbee needs help.

Postby elbeardo » Wed Dec 18, 2013 2:27 pm

Can you complete my code?.. dont know where to put your code.
elbeardo
 
Posts: 5
Joined: Wed Dec 18, 2013 1:39 pm

Re: newbee needs help.

Postby stranac » Wed Dec 18, 2013 3:15 pm

Create the StringVar where you're now assigning text = 1.

In your function, change the StringVar you created.
You'll need to use its .get() and .set() methods to do that.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: newbee needs help.

Postby elbeardo » Thu Dec 19, 2013 8:11 am

Seems i have problems. I´m using python 3.3 not python 2.7...
elbeardo
 
Posts: 5
Joined: Wed Dec 18, 2013 1:39 pm

Re: newbee needs help.

Postby stranac » Thu Dec 19, 2013 9:25 am

I'm aware of that.
Shouldn't make much of a difference.

Post what you've tried, and also what the problem is.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: newbee needs help.

Postby elbeardo » Thu Dec 19, 2013 10:50 am

This is code from "tutorialpoint", it works in P2,7 not in P3,3.

Code: Select all
from Tkinter import *

root = Tk()

var = StringVar()
label = Label( root, textvariable=var, relief=RAISED )

var.set("Hey!? How are you doing?")
label.pack()
root.mainloop()
elbeardo
 
Posts: 5
Joined: Wed Dec 18, 2013 1:39 pm

Re: newbee needs help.

Postby stranac » Thu Dec 19, 2013 10:55 am

The module is called tkinter in python 3.
I assumed you knew that, since you were using it in your first post...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: newbee needs help.

Postby Kebap » Thu Dec 19, 2013 11:06 am

Hi elBeardo!

The reason why stranac does not simply write the complete code for you? We often have people coming here, asking us to do their homework. So we would like to see some effort from everybody themselves. Nothing personal. :mrgreen:
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 375
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: newbee needs help.

Postby elbeardo » Thu Dec 19, 2013 11:16 am

whel I dont whant any homework done, I´m 33 years old and working in the construction buisnis(cant spell it, i´m from sweden.=) as a surveyor. I have made some code in RFO_Basic om android,
whana learn something else, java is to hard.. dont have the time.

And its damn hard to find simple working examples that work in P3.3

Am I on the wrong python.. is P2.7 bettter??
elbeardo
 
Posts: 5
Joined: Wed Dec 18, 2013 1:39 pm


Return to General Coding Help

Who is online

Users browsing this forum: snippsat and 2 guests