•
•
•
•
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
![]() |
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?
Where have I gone wrong? I was altering code from a tutorial.
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)
Several problems with your program, I corrected some of them:
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:
GUI/Windows programming is a mildly different world. I would get my Python basics under my belt before getting too deep into GUI.
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()# 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() Last edited by Ene Uran : Sep 7th, 2006 at 4:27 pm.
drink her pretty
It says There is no attribute Tk, even though the command is right where you put it. Here's my program for another update:
Where is my mistake this time?
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?
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation:
Rep Power: 9
Solved Threads: 173
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!
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation:
Rep Power: 9
Solved Threads: 173
You can replace console based things like print and input with popup dialog boxes, then you can use the menu you constructed ...
Sorry, had to use meter instead of metre, a habit of mine.
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() May 'the Google' be with you!
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
- Project calculator (Python)
- TKinter tough time (Python)
- What is the best language to use to use to create a menu for mobile phone???? (Computer Science and Software Design)
Other Threads in the Python Forum
- Previous Thread: what is transient()?
- Next Thread: New program: Monty Python Madlibs!



Linear Mode