Reputation: 11
I have a program where I take input from the user from entry widget and it does a function based on input. Now what has to be done so that I can take input from user inside the function?
For example
Upvotes: 0
Views: 851
Reputation: 4551
It is simple. Code:
def calculate():
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))
x = num1,num2
addition = sum(x)
print(f"The sum of the two given numbers is {addition} ")
calculate()
Upvotes: 0
Reputation: 6156
This is about as simple as it can get (not the most theme matching way but it works):
from tkinter import Tk, Button, simpledialog
def calc():
x = simpledialog.askinteger('Calculation', 'Enter x')
y = simpledialog.askinteger('Calculation', 'Enter Y')
print(x + y)
root = Tk()
Button(root, text='Calculate', command=calc).pack()
root.mainloop()
Used a Button for simplicity but you should understand how to do this the way you were doing. Also where do you plan to return that value? Certainly can't be returned back to the command
argument or the like, depends on how you have it set up (if you assign the returned value to some variable then probably fine)
Also:
I strongly suggest following PEP 8 - Style Guide for Python Code. Function and variable names should be in snake_case
, class names in CapitalCase
. Don't have space around =
if it is used as a part of keyword argument (func(arg='value')
) but have space around =
if it is used for assigning a value (variable = 'some value'
). Have space around operators (+-/
etc.: value = x + y
(except here value += x + y
)). Have two blank lines around function and class declarations.
Upvotes: 1