0

i just started learning how to code python last month and i've been trying to run this code which basically accepts some parameters calculates them and prints them out,but for some reason i cant get the result.Please any help would be appreciated

res1=int()
def converting():
    print(num1.get())
    print(form1.get())
    print(form2.get())
    mytext.set(res1)
    if form1 == 'meter' and form2=='kilometer':
        res1= print(num1*1000)
    elif form1 == 'kilometer' and form2 == 'meter':
        res1=print(num1/1000)
    elif form1 == 'gram' and form2 == 'kilogram':
        res1=print(num1*1000)
    elif form1 == 'kilogram' and form2 == 'gram' :
        res1=print(num1/1000)
    elif form1 == 'celsius' and form2 == 'fahrenheit':
        res1=print((num1*9/5)+32)
    elif form1 == 'fahrenheit' and form2 == 'celsius':
        res1=print((num1-32)*5/9)
    else:
        print('Invalid parameters')

   
ws = Tk()
ws.title("Python Guides")
ws.geometry("500x300")
mytext=StringVar()
Label(ws, text="Number").grid(row=0, sticky=W)
Label(ws, text="Initial Form").grid(row=1, sticky=W)
Label(ws, text="Conversion Form").grid(row=2, sticky=W)
Label(ws, text="Result").grid(row=3, sticky=W)
result=Label(ws, text=res1, textvariable=mytext).grid(row=3,column=1, sticky=W)

num1 = Entry(ws)
form1 = Entry(ws)
form2 = Entry(ws)


num1.grid(row=0, column=1)
form1.grid(row=1, column=1)
form2.grid(row=2, column=1)

button = Button(ws, text="Calculate", command=converting)
button.grid(row=0, column=2,columnspan=2, rowspan=2,sticky=W+E+N+S, padx=5, pady=5)


ws.mainloop()

i've checked the code and it runs fine but it doesnt give me the answer in the tkinter window instead it prints the values i've entered back on the terminal

2 Answers2

0

to solve your problem, firstly, you need to get all of the number from Entry, because you can not make math actions with them. instead of this

if form1 == 'meter' and form2=='kilometer':
    res1= print(num1*1000)

do

if form1.get() == 'meter' and form2.get()=='kilometer':
    res1= int(num1.get()) * 1000

then add mytext.set(str(res1)) inside if condition, because it is going to show error

0

Ok, I don't want to sound mean, but your code is a mess. Overloaded, not commented, it doesn't even run because you forgot the import statement/shebang line.

Then you write "i've checked the code and it runs fine but it doesnt give me the answer in the tkinter window instead it prints the values i've entered back on the terminal"

Of course it does, because that's what you do in lines 3-5.

First read this to understand what your questions should look like: https://stackoverflow.com/help/how-to-ask

res1=int()
def converting():
  print(num1.get())
  print(form1.get())
  print(form2.get())
  mytext.set(res1)

Here you have the problem of a local variable (res1 inside converting()) having the same name as the res1 variable that you defined in the first line. That doesn't work: UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)

result=Label(ws, text=res1, textvariable=mytext).grid(row=3,column=1, sticky=W)

Here you instantiate label 'result' and set the grid, which doesn't work either because the grid() function returns None: Why do I get "AttributeError: NoneType object has no attribute" using Tkinter? Where did the None value come from?

You also use

mytext=StringVar()

on a label, but you probably want to use it on the Entries.

A working example could look like this:

#!/usr/bin/env python3

from tkinter import *

def converting():
    res1 = int() # no reason to create this outside of the function
    result.config(text = res1) # set the text of Label result
    if form1.get() == 'm' and form2.get() == 'km':
        result.config(text = int(num1.get()) * 1000)
    else:
        result.config(text = 'Invalid parameters')

ws = Tk()
ws.title("Python Guides")
ws.geometry("500x300")

Label(ws, text="Number").grid(row=0, sticky=W)
Label(ws, text="Initial Form").grid(row=1, sticky=W)
Label(ws, text="Conversion Form").grid(row=2, sticky=W)
Label(ws, text="Result").grid(row=3, sticky=W)

# see link, split the line
result = Label(ws, text=0)
result.grid(row=3,column=1, sticky=W)

# the StringVars keep track of changes in the Entry
num1= StringVar()
form1 = StringVar()
form2 = StringVar()

Entry(ws, textvariable = num1).grid(row=0, column=1)
Entry(ws, textvariable = form1).grid(row=1, column=1)
Entry(ws, textvariable = form2).grid(row=2, column=1)

button = Button(ws, text="Calculate", command=converting)
button.grid(row=0, column=2,columnspan=2, rowspan=2,sticky=W+E+N+S, padx=5, pady=5)

ws.mainloop()

Here you have a nice little tutorial in which the code is much better organized: https://www.pythontutorial.net/tkinter/tkinter-stringvar/