Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

python - Tkinter application returns random values

I made a Python application using Tkinter for an electromagnetics assignment. It is a magnitude calculator for a 3-dimensional vector.

The code:

from tkinter import *

root = Tk()

root.title("Calculator")

coordinates = []

n=0

X = Entry(root)
X.insert(END, "Enter the x-coordinate")
X.grid(row=0, column=0, columnspan=3, padx=12, pady=15)

Y = Entry(root)
Y.insert(END, "Enter the y-coordinate")
Y.grid(row=1, column=0, columnspan=3, padx=12, pady=15)

Z = Entry(root)
Z.insert(END, "Enter the z-coordinate")
Z.grid(row=2, column=0, columnspan=3, padx=12, pady=15)

def x():
    global n
    n=1
    
def y():
    global n
    n=2
    
def z():
    global n
    n=3
    
def S(m):
    if n == 1:
        a = X.get()
        X.delete(0,END)
        X.insert(0,str(a)+str(m))
        coordinates.append(X)
    if n == 2:
        b = Y.get()
        Y.delete(0,END)
        Y.insert(0,str(b)+str(m))
        coordinates.append(Y)
    if n == 3:
        c = Z.get()
        Z.delete(0,END)
        Z.insert(0,str(c)+str(m))
        coordinates.append(Z)
        
def clear():
    X.delete(0,END)
    Y.delete(0,END)
    Z.delete(0,END)
    
def equal():
    sq = 0
    for d in coordinates:
        e = float(d.get())
        sq = sq + e**2 
    mag = sq**0.5
    val = Label(root, text="The magnitude of the vector is: " + str(mag))
    val.grid(row=3, column=0, columnspan=3, padx=12, pady=15)
    
b1 = Button(root, text="1", padx=30, pady=15, command=lambda: S("1"))
b2 = Button(root, text="2", padx=30, pady=15, command=lambda: S("2"))
b3 = Button(root, text="3", padx=30, pady=15, command=lambda: S("3"))
b4 = Button(root, text="4", padx=30, pady=15, command=lambda: S("4"))
b5 = Button(root, text="5", padx=30, pady=15, command=lambda: S("5"))
b6 = Button(root, text="6", padx=30, pady=15, command=lambda: S("6"))
b7 = Button(root, text="7", padx=30, pady=15, command=lambda: S("7"))
b8 = Button(root, text="8", padx=30, pady=15, command=lambda: S("8"))
b9 = Button(root, text="9", padx=30, pady=15, command=lambda: S("9"))
b0 = Button(root, text="0", padx=30, pady=15, command=lambda: S("0"))
bx = Button(root, text="x", padx=30, pady=15, command=x)
by = Button(root, text="y", padx=30, pady=15, command=y)
bz = Button(root, text="z", padx=30, pady=15, command=z)
bclear = Button(root, text="Clear", padx=19, pady=15, command=clear)
bequal = Button(root, text="=", padx=29, pady=15, command=equal)

b1.grid(row=6, column=0)
b2.grid(row=6, column=1)
b3.grid(row=6, column=2)
b4.grid(row=5, column=0)
b5.grid(row=5, column=1)
b6.grid(row=5, column=2)
b7.grid(row=4, column=0)
b8.grid(row=4, column=1)
b9.grid(row=4, column=2)
b0.grid(row=7, column=1)
bx.grid(row=8, column=0)
by.grid(row=8, column=1)
bz.grid(row=8, column=2)
bclear.grid(row=7, column=2)
bequal.grid(row=7, column=0)

root.mainloop()

There seems to be just the one problem:

When the user enters a value greater than 10 for even one of the inputs, the output seems to be greater than the actual value. It only works for very small inputs, for instance, if the numbers entered were 3, 4, 5.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well the code structure was a bit too harsh on me, so I had to think of other logical ways but ended up getting a hacky way around.

I've commented the code so as to make the understand easier on-the-go:

from tkinter import *

root = Tk()
root.title("Calculator")

coordinates = [] # making empty list to hold all the numbers
xi,yi,zi = [],[],[] #making empty list to hold only the corresponding numbers

n = 0 #initial value of n

X = Entry(root)
X.grid(row=0, column=0, columnspan=3, padx=12, pady=15)

Y = Entry(root)
Y.grid(row=1, column=0, columnspan=3, padx=12, pady=15)

Z = Entry(root)
Z.grid(row=2, column=0, columnspan=3, padx=12, pady=15)

def x():
    global n
    n = 1
    
def y():
    global n
    n = 2
    
def z():
    global n
    n = 3
    
def S(m):
    if n == 1: #if n is 1
        X.insert(END,str(m)) #then insert the clicked number to the end of the corresponding entry box 
        coordinates.append(X.get()) #append to the main list of all numbers 
    if n == 2:
        Y.insert(END,str(m)) #doing same as above for y
        coordinates.append(Y.get())
    if n == 3:
        Z.insert(END,str(m)) #doing same as above for z
        coordinates.append(Z.get())

def clear():
    #removing all the entry widget 
    X.delete(0,END)
    Y.delete(0,END)
    Z.delete(0,END)

    #clearing all the lists
    coordinates.clear()
    xi.clear()
    yi.clear()
    zi.clear()
    
def equal():
    xi.append(coordinates[len(X.get())-1]) #get the last entered value from the entire list of numbers for x
    yi.append(coordinates[len(Y.get())+len(X.get())-1]) #get the last entered value from the entire list of numbers for y
    zi.append(coordinates[len(Z.get())+len(X.get())+len(Y.get())-1]) #get the last entered value from the entire list of numbers for z
    final_coordinates = xi+yi+zi #make a new final list that has the cordinates only

    sq = 0
    for d in final_coordinates:
        e = float(d)
        sq = sq + e**2 
    mag = sq**0.5
    val.config(text=f"The magnitude of the vector is: {round(mag,3)}") #doing this to get rid of overwriting of labels
    val.grid(row=3, column=0, columnspan=4, padx=12, pady=15)
    
b1 = Button(root, text="1", padx=30, pady=15, command=lambda: S(1))
b2 = Button(root, text="2", padx=30, pady=15, command=lambda: S(2))
b3 = Button(root, text="3", padx=30, pady=15, command=lambda: S(3))
b4 = Button(root, text="4", padx=30, pady=15, command=lambda: S(4))
b5 = Button(root, text="5", padx=30, pady=15, command=lambda: S(5))
b6 = Button(root, text="6", padx=30, pady=15, command=lambda: S(6))
b7 = Button(root, text="7", padx=30, pady=15, command=lambda: S(7))
b8 = Button(root, text="8", padx=30, pady=15, command=lambda: S(8))
b9 = Button(root, text="9", padx=30, pady=15, command=lambda: S(9))
b0 = Button(root, text="0", padx=30, pady=15, command=lambda: S(0))
bx = Button(root, text="x", padx=30, pady=15, command=x)
by = Button(root, text="y", padx=30, pady=15, command=y)
bz = Button(root, text="z", padx=30, pady=15, command=z)
bclear = Button(root, text="Clear", padx=19, pady=15, command=clear)
bequal = Button(root, text="=", padx=29, pady=15, command=equal)

b1.grid(row=6, column=0)
b2.grid(row=6, column=1)
b3.grid(row=6, column=2)
b4.grid(row=5, column=0)
b5.grid(row=5, column=1)
b6.grid(row=5, column=2)
b7.grid(row=4, column=0)
b8.grid(row=4, column=1)
b9.grid(row=4, column=2)
b0.grid(row=7, column=1)
bx.grid(row=8, column=0)
by.grid(row=8, column=1)
bz.grid(row=8, column=2)
bclear.grid(row=7, column=2)
bequal.grid(row=7, column=0)

val = Label(root) #declaring here, so that label can be configured later 

root.mainloop()

Explanation:

Imagine, we entered 123 in the x box, 852 in the y box and 369 in the z box, so the final list, coordinates, after appending all x, y, z is

['1', '12', '123', '8', '85', '852', '3', '36', '369']

So now the length of entry on x is 3, and that is our 3rd item on the list. Similarly, the length of the entry on y is 3, that is the 6th item on the list(which is length of x + length of y), same applies to z too. Now since we are indexing it, we will go 1 down(subtract 1) always, because indexing starts from 0, whereas our counting starts from 1.

Also, I removed the placeholders from inside the entry box as it can cause inefficiency, maybe just put a label on top of each entry, stating whats the purpose of the entry widget. Also I have used round(mag,3) to round the digit upto 3 decimal values, since I gets awkward after that(for me), you could just get rid of it and have the entire numbers spit out.

Hope this gets the job done, but do let me know if any doubts.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...