Python Forum
Pass 2x Variables from Function to another Function
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass 2x Variables from Function to another Function
#1
I am trying to avoid using Global variables to solve the problem i am having of passing 2 variables from one function to another function.

Function 1 is an 'Accept Button' taking some user input information from input fields and producing upon pressing accept Variable_1 and Variable_2

Function 2 is a 'Test Button' taking Variable_1 and Variable_2 and using them as inputs for the test code

How do i get Variable 1 & 2 into my second function without setting them as globals?
Reply
#2
def function1(): var1 = 1 var2 = 5 result = function2(var1, var2) print(result) def function2(num1, num2): return num1 + num2 function1()
in the function definition you can specify positional and/or keyword parameters. Then you will pass actual values as arguments when you call the function
http://stupidpythonideas.blogspot.com/20...eters.html
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
You just pass them as two parameters:

def first(a): ab = a + 'b' ac = a + 'c' second(ab, ac) def second(d, e): print('{} then {}'.format(d, e))
Edit: whoops, Buran beat me to it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Aug-14-2018, 01:52 PM)buran Wrote:
def function1(): var1 = 1 var2 = 5 result = function2(var1, var2) print(result) def function2(num1, num2): return num1 + num2 function1()
in the function definition you can specify positional and/or keyword parameters. Then you will pass actual values as arguments when you call the function
http://stupidpythonideas.blogspot.com/20...eters.html

I have tried this but i get errors when i try integrating it into my code.

def entry_field_accept(): Output_IP_Address1 = "00.000.000.1" Output_IP_Address2 = "00.000.000.2" Output_Result = IPP_Test(Output_IP_Address1, Output_IP_Address2) print(Output_Result)
def IPP_Test(Output_IP_Address1, Output_IP_Address2): print("Test Start") return Output_IP_Address1 + Output_IP_Address2 print("Ping Test: ", Output_IP_Address1) print("Ping Test:", Output_IP_Address2)
TypeError: IPP_Test() missing 2 required positional arguments: 'Output_IP_Address1'and'Output_IP_Address2'

If i then change my TkInter code and add the args

IPTest_Button = tk.Button(IPP, text = 'Test', command = IPP_Test(Output_IP_Address1, Output_IP_Address2), background ="#01AEAC", width=12, height=2,)
NameError: name 'Output_IP_Address1 is not defined

any ideas?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 3,546 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  How to pass encrypted pass to pyodbc script tester_V 0 2,330 Jul-27-2023, 12:40 AM
Last Post: tester_V
  How to print variables in function? samuelbachorik 3 2,594 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  User-defined function to reset variables? Mark17 3 4,710 May-25-2022, 07:22 PM
Last Post: Gribouillis
  Exit function from nested function based on user input Turtle 5 5,618 Oct-10-2021, 12:55 AM
Last Post: Turtle
  How to pass variables from one class to another hobbyist 18 24,307 Oct-01-2021, 05:54 PM
Last Post: deanhystad
  Regex - Pass Flags as a function argument? muzikman 6 7,782 Sep-06-2021, 03:43 PM
Last Post: muzikman
Question Stopping a parent function from a nested function? wallgraffiti 1 5,248 May-02-2021, 12:21 PM
Last Post: Gribouillis
Question exiting the outer function from the inner function banidjamali 3 7,131 Feb-27-2021, 09:47 AM
Last Post: banidjamali
  Possible to dynamically pass arguments to a function? grimm1111 2 3,581 Feb-21-2021, 05:57 AM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.