I have been working on an app in python for a teacher at my school. What I want it to do is have a simple GUI and a way I can type in a word and it will print the words value in the same slot i typed the word.(kind of like a calculator) A=1 B=2 C=3 etc. It is fairly simple, as i am a beginner, but I can't quite get my button to show the value of the words I type in. If anyone could help it would be great! Thanks! Here is my code so far:
from Tkinter import *
import sys
def dollaramount():
print sum(map(" abcdefghijklmnopqrstuvwxyz".index, raw_input().lower()))
root = Tk()
frame = Frame(root)
frame.pack()
num1=StringVar()
topframe = Frame(root)
topframe.pack(side=TOP)
txtDisplay=Entry(frame, textvariable = num1, bd= 20, insertwidth= 1, font= 30, bg="white", fg="black")
txtDisplay.pack(side=TOP)
button1 = Button(topframe, padx=16, pady=16, bd=8, text="=", bg="white", fg="black", command=dollaramount)
button1.pack(side=LEFT)
root.mainloop()
I guess what you want is the following:
from Tkinter import *
import sys
def dollaramount():
# get the word written in the entry
word = num1.get()
# compute its value
val = sum(map(" abcdefghijklmnopqrstuvwxyz".index, word.lower()))
# display its value in the entry
num1.set("%s=%i" % (word, val))
root = Tk()
frame = Frame(root)
frame.pack()
num1=StringVar(root)
topframe = Frame(root)
topframe.pack(side=TOP)
txtDisplay=Entry(frame, textvariable = num1, bd= 20, insertwidth= 1, font= 30, bg="white", fg="black")
txtDisplay.pack(side=TOP)
button1 = Button(topframe, padx=16, pady=16, bd=8, text="=", bg="white", fg="black", command=dollaramount)
button1.pack(side=LEFT)
root.mainloop()
Your dollaramount
function was not working because you used command line specific functions print
and raw_input
instead of using the set
and get
methods of the StringVar. I commented the code so that you can understand what it does.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments