User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 397,803 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,471 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 1322 | Replies: 6
Reply
Join Date: Sep 2006
Posts: 102
Reputation: chris99 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Trying to learn Tkinter. Want help with the menu:

  #1  
Sep 7th, 2006
The code won't even display the root, let alone the menus. Even when the conversion works, nothing else does, so what do I do?

from Tkinter import *
root = Tk()
def hello():
    print "Hello!"
def metres_to_centimetres(metre,cm):
    metre = input("Put in a distance in metres: ")
    cm = metre*100
    print "Distance in Centimetres = ",cm
 
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Hello", command=hello)
filemenu.add_command(label="M to Cm", command=metres_to_centimetres(metre,cm))
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
mainloop()

Where have I gone wrong? I was altering code from a tutorial.
Last edited by chris99 : Sep 7th, 2006 at 5:10 am. Reason: misspelling(gone)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2006
Posts: 102
Reputation: chris99 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Re: Trying to learn Tkinter. Want help with the menu:

  #2  
Sep 7th, 2006
To update the problem, the "def metres_to_centimetres" runs first, then the gui displays, then the "M to cm" button does nothing while "hello" works perfectly.
Reply With Quote  
Join Date: Aug 2005
Posts: 1,020
Reputation: Ene Uran is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 64
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Veteran Poster

Re: Trying to learn Tkinter. Want help with the menu:

  #3  
Sep 7th, 2006
Several problems with your program, I corrected some of them:
from Tkinter import *

root = Tk()

def hello():
    print "Hello!"

# removed unneeded arguments (metre,cm)
def metres_to_centimetres():
    metre = input("Put in a distance in metres: ")
    cm = metre*100
    print "Distance in Centimetres = ",cm
 
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Hello", command=hello)

# command accepts a function object but not a function call
# to pass any arguments you have to wrap the object in lambda
#filemenu.add_command(label="M to Cm", command=metres_to_centimetres(metre,cm))  # gives error
# actually you don't need to pass any arguments
filemenu.add_command(label="M to Cm", command=metres_to_centimetres)

filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
mainloop()
Now the major problem is that you are mixing console only functions like print and input with a GUI environment. In a GUI program you use widget Label for prompts and results, and widget Entry for data entry/input or display. Actions are generally done with a button click. Here is an example:
# example of a labeled data entry, a button and a result label
# use grid to position widgets, also set cursor to focus on entry
# a rather generic Tkinter GUI template for calculations

from Tkinter import *

def calculate():
    try:
        # get the enter1 value
        # use float() for either float or integer entry
        num1 = float(enter1.get())
        # do the calculation
        result = num1 * 100
        # display the result string
        # use str() to convert numbers to string for concatination with +
        label2.config(text=str(num1) + 'meters = ' + str(result) + 'cm')
    except ValueError:
        label2.config(text='Enter a numeric value!')

root = Tk()

# data entry with a prompt label
label1 = Label(root, text='Enter distance in meters:')
label1.grid(row=0, column=0)
enter1 = Entry(root, bg='yellow')
enter1.grid(row=1, column=0)

# do the calculation by clicking the button
btn1 = Button(root, text='Calculate Result', command=calculate)
btn1.grid(row=2, column=0)

# display the result in this label
label2 = Label(root, bg='green')
label2.grid(row=3, column=0)

# start cursor in enter1
enter1.focus()

root.mainloop()
GUI/Windows programming is a mildly different world. I would get my Python basics under my belt before getting too deep into GUI.
Last edited by Ene Uran : Sep 7th, 2006 at 4:27 pm.
drink her pretty
Reply With Quote  
Join Date: Sep 2006
Posts: 102
Reputation: chris99 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Re: Trying to learn Tkinter. Want help with the menu:

  #4  
Sep 8th, 2006
It says There is no attribute Tk, even though the command is right where you put it. Here's my program for another update:

from Tkinter import *
def calculate():
    try:
        metre = float(enter1.get())
        result = metre * 100
        label2 = Label(calculate, bg="blue")
        label2.config(text=str(metre)+ 'meters = ' + str(result)+'cm')
    except ValueError:
        label2.config(text="Enter a numeric value!")
    
root = Tk()
                      
label1 = Label(root, text="Enter distance in metres: ")
label1.grid(row=0, column=0)
enter1 = Entry(root, bg="Yellow")
enter1.grid(row=1, column=0)
btnl = Button(root, text="Calculate Result", command=calculate)
btnl.grid(row=2, column=0)
mainloop()

Where is my mistake this time?
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: Trying to learn Tkinter. Want help with the menu:

  #5  
Sep 8th, 2006
See my comments in the code ...
from Tkinter import *
def calculate():
    try:
        metre = float(enter1.get())
        result = metre * 100
        #label2 = Label(calculate, bg="blue")  # error here
        label2 = Label(root, bg="blue")        # black text on blue is hard to see!
        label2.grid(row=3, column=0)           # this was missing
        label2.config(text=str(metre)+ 'meters = ' + str(result)+'cm')
    except ValueError:
        label2.config(text="Enter a numeric value!")
    
root = Tk()
                      
label1 = Label(root, text="Enter distance in metres: ")
label1.grid(row=0, column=0)
enter1 = Entry(root, bg="Yellow")
enter1.grid(row=1, column=0)
btnl = Button(root, text="Calculate Result", command=calculate)
btnl.grid(row=2, column=0)
mainloop()
May 'the Google' be with you!
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: Trying to learn Tkinter. Want help with the menu:

  #6  
Sep 8th, 2006
You can replace console based things like print and input with popup dialog boxes, then you can use the menu you constructed ...
from Tkinter import *
from tkMessageBox   import showinfo
from tkSimpleDialog import askfloat

root = Tk()

def hello():
    # print "Hello!"  # console only
    showinfo(None, "Hello!")
    
def meters_to_centimeters():
    # meter = input("Put in a distance in meters: ")  # console only
    meter = askfloat(None, "Put in a distance in meters:")
    cm = meter * 100
    # print "Distance in Centimeters = ",cm
    showinfo(None, "Distance in Centimeters = " + str(cm))
 
menubar = Menu(root)
filemenu = Menu(menubar,  tearoff=0)
filemenu.add_command(label="Hello", command=hello)
filemenu.add_command(label="M to Cm", command=meters_to_centimeters)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
root.mainloop()
Sorry, had to use meter instead of metre, a habit of mine.
May 'the Google' be with you!
Reply With Quote  
Join Date: Sep 2006
Posts: 102
Reputation: chris99 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Re: Trying to learn Tkinter. Want help with the menu:

  #7  
Sep 8th, 2006
Thanks everyone. I'll look out for the submissions Ene Uran submits into the Beginner programs thread.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 6:13 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC